fn closures in RBE
This commit is contained in:
parent
aa83db6f7a
commit
7e0aa3b86b
36
rustbyexample/fn.d/closures.d/capture.rs
Normal file
36
rustbyexample/fn.d/closures.d/capture.rs
Normal file
@ -0,0 +1,36 @@
|
||||
fn main() {
|
||||
use std::mem;
|
||||
|
||||
let color = String::from("green");
|
||||
|
||||
let print = || println!("`color`: {}", color);
|
||||
|
||||
print();
|
||||
|
||||
let _reborrow = &color;
|
||||
print();
|
||||
|
||||
let _color_moved = color;
|
||||
|
||||
let mut count = 0;
|
||||
|
||||
let mut inc = || {
|
||||
count += 1;
|
||||
println!("`count`: {}", count);
|
||||
};
|
||||
|
||||
inc();
|
||||
|
||||
inc();
|
||||
|
||||
let _count_reborrowed = &mut count;
|
||||
|
||||
let movable = Box::new(3);
|
||||
|
||||
let consume = || {
|
||||
println!("`movable`: {:?}", movable);
|
||||
mem::drop(movable);
|
||||
};
|
||||
|
||||
consume();
|
||||
}
|
8
rustbyexample/fn.d/closures.d/capture2.rs
Normal file
8
rustbyexample/fn.d/closures.d/capture2.rs
Normal file
@ -0,0 +1,8 @@
|
||||
fn main() {
|
||||
let haystack = vec![1, 2, 3];
|
||||
|
||||
let contains = move |needle| haystack.contains(needle);
|
||||
|
||||
println!("{}", contains(&1));
|
||||
println!("{}", contains(&4));
|
||||
}
|
Loading…
Reference in New Issue
Block a user