]>
Commit | Line | Data |
---|---|---|
136023e0 | 1 | // edition:2021 |
fc512014 XL |
2 | // run-pass |
3 | ||
4 | // Test whether if we can do precise capture when using nested clsoure. | |
5 | ||
fc512014 XL |
6 | struct Point { |
7 | x: i32, | |
8 | y: i32, | |
9 | } | |
10 | ||
11 | fn main() { | |
12 | let mut p = Point { x: 5, y: 20 }; | |
13 | ||
14 | // c1 should capture `p.x` via immutable borrow and | |
15 | // `p.y` via mutable borrow. | |
16 | let mut c1 = || { | |
17 | println!("{}", p.x); | |
18 | ||
19 | let incr = 10; | |
20 | ||
21 | let mut c2 = || p.y += incr; | |
22 | c2(); | |
23 | ||
24 | println!("{}", p.y); | |
25 | }; | |
26 | ||
27 | c1(); | |
28 | ||
29 | // This should not throw an error because `p.x` is borrowed via Immutable borrow, | |
30 | // and multiple immutable borrow of the same place are allowed. | |
31 | let px = &p.x; | |
32 | ||
33 | println!("{}", px); | |
34 | ||
35 | c1(); | |
36 | } |