]> git.proxmox.com Git - rustc.git/blame - src/test/run-pass/closure-reform.rs
Imported Upstream version 1.1.0+dfsg1
[rustc.git] / src / test / run-pass / closure-reform.rs
CommitLineData
1a4d82fc
JJ
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
d9579d0f 14#![feature(unboxed_closures)]
1a4d82fc
JJ
15
16fn call_it<F>(f: F)
17 where F : FnOnce(String) -> String
18{
19 println!("{}", f("Fred".to_string()))
20}
21
22fn call_a_thunk<F>(f: F) where F: FnOnce() {
23 f();
24}
25
26fn call_this<F>(f: F) where F: FnOnce(&str) + Send {
27 f("Hello!");
28}
29
30fn call_bare(f: fn(&str)) {
31 f("Hello world!")
32}
33
34fn call_bare_again(f: extern "Rust" fn(&str)) {
35 f("Goodbye world!")
36}
37
38pub fn main() {
39 // Procs
40
41 let greeting = "Hello ".to_string();
42 call_it(|s| {
43 format!("{}{}", greeting, s)
44 });
45
46 let greeting = "Goodbye ".to_string();
47 call_it(|s| format!("{}{}", greeting, s));
48
49 let greeting = "How's life, ".to_string();
50 call_it(|s: String| -> String {
51 format!("{}{}", greeting, s)
52 });
53
54 // Closures
55
56 call_a_thunk(|| println!("Hello world!"));
57
58 call_this(|s| println!("{}", s));
59
60 // External functions
61
9346a6ac
AL
62 fn foo(s: &str) {}
63 call_bare(foo);
1a4d82fc 64
9346a6ac 65 call_bare_again(foo);
1a4d82fc 66}