]> git.lizzy.rs Git - rust.git/blob - tests/ui/functions-closures/closure-reform.rs
Rollup merge of #106113 - krasimirgg:llvm-16-ext-tyid, r=nikic
[rust.git] / tests / ui / functions-closures / closure-reform.rs
1 // run-pass
2 #![allow(unused_variables)]
3 /* Any copyright is dedicated to the Public Domain.
4  * http://creativecommons.org/publicdomain/zero/1.0/ */
5
6 fn call_it<F>(f: F)
7     where F : FnOnce(String) -> String
8 {
9     println!("{}", f("Fred".to_string()))
10 }
11
12 fn call_a_thunk<F>(f: F) where F: FnOnce() {
13     f();
14 }
15
16 fn call_this<F>(f: F) where F: FnOnce(&str) + Send {
17     f("Hello!");
18 }
19
20 fn call_bare(f: fn(&str)) {
21     f("Hello world!")
22 }
23
24 fn call_bare_again(f: extern "Rust" fn(&str)) {
25     f("Goodbye world!")
26 }
27
28 pub fn main() {
29     // Procs
30
31     let greeting = "Hello ".to_string();
32     call_it(|s| {
33         format!("{}{}", greeting, s)
34     });
35
36     let greeting = "Goodbye ".to_string();
37     call_it(|s| format!("{}{}", greeting, s));
38
39     let greeting = "How's life, ".to_string();
40     call_it(|s: String| -> String {
41         format!("{}{}", greeting, s)
42     });
43
44     // Closures
45
46     call_a_thunk(|| println!("Hello world!"));
47
48     call_this(|s| println!("{}", s));
49
50     // External functions
51
52     fn foo(s: &str) {}
53     call_bare(foo);
54
55     call_bare_again(foo);
56 }