-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclosures_capturing.rs
73 lines (57 loc) · 2.26 KB
/
closures_capturing.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
fn main() {
use std::mem;
let color = "green";
// A closure to print `color` which immediately borrows (`&`)
// `color` and stores the borrow and closure in the `print`
// variable. It will remain borrowed until `print` goes out of
// score. `println!` only requires `by reference` so it doesn't
// impose anything more restrictive.
let print = || println!("`color`: {}", color);
// Call the closure using the borrow.
print();
print();
let mut count = 0;
// A closure to increment `count` could take either `&mut count`
// or `count` but `&mut count` is less restrictive so it takes
// that. Immediately borrows `count`.
//
// A `mut` is required on `inc` because a `&mut` is stored inside.
// Thus, calling the closure mutates the closure which requires
// a `mut`.
let mut inc = || {
count += 1;
println!("`count`: {}", count);
};
// Call the closure.
inc();
inc();
// let _reborrow = &mut count;
// ^ TODO try uncommenting this line.
// A non-copy type.
let movable = Box::new(3);
// `mem:drop` requires `T` so this must take by value. A copy type
// would copy into the closure leaving the original untouched.
// A non-copy must move and so `movable` immediately moves into
// the closure.
let consume = || {
println!("`movable`: {:?}", movable);
mem::drop(movable);
};
// `consume` consumes the variable so this can only be called once.
consume();
//consume();
// ^ TODO: Try uncommenting this line.
// `Vec` has non-copy semantics.
let haystack = vec![1, 2, 3];
// let contains = move |needle| haystack.contains(needle);
let contains = |needle| haystack.contains(needle);
println!("{}", contains(&1));
println!("{}", contains(&4));
println!("There're {} elements in vec", haystack.len());
// ^ Uncommenting above line will results in a compile-time error
// because borrow checker doesn't allow re-using variable after it
// has been moved.
// Removing `move` from closure's signature will cause closure
// to borrow _haystack_ variable immutably, hence _haystack_ is still
// available and uncommenting line will not cause and error.
}