2018-08-30 12:18:55 +00:00
|
|
|
// run-pass
|
2018-09-25 21:51:35 +00:00
|
|
|
#![allow(unused_variables)]
|
2013-10-28 22:22:49 +00:00
|
|
|
/* Any copyright is dedicated to the Public Domain.
|
|
|
|
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
|
|
|
|
2014-11-26 13:12:18 +00:00
|
|
|
fn call_it<F>(f: F)
|
|
|
|
where F : FnOnce(String) -> String
|
|
|
|
{
|
2014-05-25 10:17:19 +00:00
|
|
|
println!("{}", f("Fred".to_string()))
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 22:32:54 +00:00
|
|
|
fn call_a_thunk<F>(f: F) where F: FnOnce() {
|
2013-10-29 22:06:13 +00:00
|
|
|
f();
|
|
|
|
}
|
|
|
|
|
2015-01-02 22:32:54 +00:00
|
|
|
fn call_this<F>(f: F) where F: FnOnce(&str) + Send {
|
2013-10-29 22:06:13 +00:00
|
|
|
f("Hello!");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_bare(f: fn(&str)) {
|
|
|
|
f("Hello world!")
|
|
|
|
}
|
|
|
|
|
|
|
|
fn call_bare_again(f: extern "Rust" fn(&str)) {
|
|
|
|
f("Goodbye world!")
|
|
|
|
}
|
|
|
|
|
2013-10-28 22:22:49 +00:00
|
|
|
pub fn main() {
|
2013-10-29 22:06:13 +00:00
|
|
|
// Procs
|
|
|
|
|
2014-05-25 10:17:19 +00:00
|
|
|
let greeting = "Hello ".to_string();
|
2014-11-26 13:12:18 +00:00
|
|
|
call_it(|s| {
|
2014-05-28 03:44:58 +00:00
|
|
|
format!("{}{}", greeting, s)
|
2013-10-28 22:22:49 +00:00
|
|
|
});
|
|
|
|
|
2014-05-25 10:17:19 +00:00
|
|
|
let greeting = "Goodbye ".to_string();
|
2014-11-26 13:12:18 +00:00
|
|
|
call_it(|s| format!("{}{}", greeting, s));
|
2013-10-28 22:22:49 +00:00
|
|
|
|
2014-05-25 10:17:19 +00:00
|
|
|
let greeting = "How's life, ".to_string();
|
2014-11-26 13:12:18 +00:00
|
|
|
call_it(|s: String| -> String {
|
2014-05-28 03:44:58 +00:00
|
|
|
format!("{}{}", greeting, s)
|
2013-10-28 22:22:49 +00:00
|
|
|
});
|
2013-10-29 22:06:13 +00:00
|
|
|
|
|
|
|
// Closures
|
|
|
|
|
2014-01-09 10:06:55 +00:00
|
|
|
call_a_thunk(|| println!("Hello world!"));
|
2013-10-29 22:06:13 +00:00
|
|
|
|
2014-01-09 10:06:55 +00:00
|
|
|
call_this(|s| println!("{}", s));
|
2013-10-29 22:06:13 +00:00
|
|
|
|
|
|
|
// External functions
|
|
|
|
|
2015-04-10 18:12:43 +00:00
|
|
|
fn foo(s: &str) {}
|
|
|
|
call_bare(foo);
|
2013-10-29 22:06:13 +00:00
|
|
|
|
2015-04-10 18:12:43 +00:00
|
|
|
call_bare_again(foo);
|
2013-10-28 22:22:49 +00:00
|
|
|
}
|