A few more RBE things
This commit is contained in:
parent
0e6448df23
commit
087355502d
14
rustbyexample/fn.d/closures.d/anonymity.rs
Normal file
14
rustbyexample/fn.d/closures.d/anonymity.rs
Normal file
@ -0,0 +1,14 @@
|
||||
fn apply<F>(f: F)
|
||||
where
|
||||
F: Fn(),
|
||||
{
|
||||
f();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let x = 7;
|
||||
|
||||
let print = || println!("{}", x);
|
||||
|
||||
apply(print);
|
||||
}
|
36
rustbyexample/fn.d/closures.d/input_parameters.rs
Normal file
36
rustbyexample/fn.d/closures.d/input_parameters.rs
Normal file
@ -0,0 +1,36 @@
|
||||
fn apply<F>(f: F)
|
||||
where
|
||||
F: FnOnce(),
|
||||
{
|
||||
f();
|
||||
}
|
||||
|
||||
fn apply_to_3<F>(f: F) -> i32
|
||||
where
|
||||
F: Fn(i32) -> i32,
|
||||
{
|
||||
f(3)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
use std::mem;
|
||||
|
||||
let greeting = "hello";
|
||||
let mut farewell = "goodbye".to_owned();
|
||||
|
||||
let diary = || {
|
||||
println!("I said {}.", greeting);
|
||||
|
||||
farewell.push_str("!!!");
|
||||
println!("Then I screamed {}.", farewell);
|
||||
println!("Now I can sleep. zzzzz");
|
||||
|
||||
mem::drop(farewell);
|
||||
};
|
||||
|
||||
apply(diary);
|
||||
|
||||
let double = |x| 2 * x;
|
||||
|
||||
println!("3 doubled: {}", apply_to_3(double));
|
||||
}
|
Loading…
Reference in New Issue
Block a user