@@ -18,37 +18,51 @@ fn main() {
18
18
19
19
let color = "green";
20
20
21
- // A closure to print `color` which immediately borrows (`&`)
22
- // `color` and stores the borrow and closure in the `print`
23
- // variable. It will remain borrowed until `print` goes out of
24
- // scope. `println!` only requires `by reference` so it doesn't
21
+ // A closure to print `color` which immediately borrows (`&`) `color` and
22
+ // stores the borrow and closure in the `print` variable. It will remain
23
+ // borrowed until `print` is used the last time.
24
+ //
25
+ // `println!` only requires arguments by immutable reference so it doesn't
25
26
// impose anything more restrictive.
26
27
let print = || println!("`color`: {}", color);
27
28
28
29
// Call the closure using the borrow.
29
30
print();
31
+
32
+ // `color` can be borrowed immutably again, because the closure only holds
33
+ // an immutable reference to `color`.
34
+ let _reborrow = &color;
30
35
print();
31
36
32
- let mut count = 0;
37
+ // A move or reborrow is allowed after the final use of `print`
38
+ let _color_moved = color;
33
39
34
- // A closure to increment `count` could take either `&mut count`
35
- // or `count` but `&mut count` is less restrictive so it takes
36
- // that. Immediately borrows `count`.
40
+
41
+ let mut count = 0;
42
+ // A closure to increment `count` could take either `&mut count` or `count`
43
+ // but `&mut count` is less restrictive so it takes that. Immediately
44
+ // borrows `count`.
37
45
//
38
- // A `mut` is required on `inc` because a `&mut` is stored inside.
39
- // Thus, calling the closure mutates the closure which requires
40
- // a `mut`.
46
+ // A `mut` is required on `inc` because a `&mut` is stored inside. Thus,
47
+ // calling the closure mutates the closure which requires a `mut`.
41
48
let mut inc = || {
42
49
count += 1;
43
50
println!("`count`: {}", count);
44
51
};
45
52
46
- // Call the closure.
47
- inc();
53
+ // Call the closure using a mutable borrow.
48
54
inc();
49
55
50
- //let _reborrow = &mut count;
56
+ // The closure still mutably borrows `count` because it is called later.
57
+ // An attempt to reborrow will lead to an error.
58
+ // let _reborrow = &count;
51
59
// ^ TODO: try uncommenting this line.
60
+ inc();
61
+
62
+ // The closure no longer needs to borrow `&mut count`. Therefore, it is
63
+ // possible to reborrow without an error
64
+ let _count_reborrowed = &mut count;
65
+
52
66
53
67
// A non-copy type.
54
68
let movable = Box::new(3);
@@ -64,7 +78,7 @@ fn main() {
64
78
65
79
// `consume` consumes the variable so this can only be called once.
66
80
consume();
67
- //consume();
81
+ // consume();
68
82
// ^ TODO: Try uncommenting this line.
69
83
}
70
84
```
@@ -82,7 +96,7 @@ fn main() {
82
96
println!("{}", contains(&1));
83
97
println!("{}", contains(&4));
84
98
85
- // ` println!("There're {} elements in vec", haystack.len());`
99
+ // println!("There're {} elements in vec", haystack.len());
86
100
// ^ Uncommenting above line will result in compile-time error
87
101
// because borrow checker doesn't allow re-using variable after it
88
102
// has been moved.
0 commit comments