]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/closure-reform.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / closure-reform.rs
1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /* Any copyright is dedicated to the Public Domain.
12  * http://creativecommons.org/publicdomain/zero/1.0/ */
13
14 #![feature(unboxed_closures)]
15
16 use std::mem;
17 use std::io::stdio::println;
18
19 fn call_it<F>(f: F)
20     where F : FnOnce(String) -> String
21 {
22     println!("{}", f("Fred".to_string()))
23 }
24
25 fn call_a_thunk<F>(f: F) where F: FnOnce() {
26     f();
27 }
28
29 fn call_this<F>(f: F) where F: FnOnce(&str) + Send {
30     f("Hello!");
31 }
32
33 fn call_bare(f: fn(&str)) {
34     f("Hello world!")
35 }
36
37 fn call_bare_again(f: extern "Rust" fn(&str)) {
38     f("Goodbye world!")
39 }
40
41 pub fn main() {
42     // Procs
43
44     let greeting = "Hello ".to_string();
45     call_it(|s| {
46         format!("{}{}", greeting, s)
47     });
48
49     let greeting = "Goodbye ".to_string();
50     call_it(|s| format!("{}{}", greeting, s));
51
52     let greeting = "How's life, ".to_string();
53     call_it(|s: String| -> String {
54         format!("{}{}", greeting, s)
55     });
56
57     // Closures
58
59     call_a_thunk(|| println!("Hello world!"));
60
61     call_this(|s| println!("{}", s));
62
63     // External functions
64
65     call_bare(println);
66
67     call_bare_again(println);
68 }
69