summaryrefslogtreecommitdiffstats
path: root/src/test/ui/closures/2229_closure_analysis/run_pass/nested-closure.rs
blob: a80b40bb46957b8f871af21d720e43d1c326f691 (plain)
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
// edition:2021
// run-pass

// Test whether if we can do precise capture when using nested clsoure.

struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let mut p = Point { x: 5, y: 20 };

    // c1 should capture `p.x` via immutable borrow and
    // `p.y` via mutable borrow.
    let mut c1 = || {
        println!("{}", p.x);

        let incr = 10;

        let mut c2 = || p.y += incr;
        c2();

        println!("{}", p.y);
    };

    c1();

    // This should not throw an error because `p.x` is borrowed via Immutable borrow,
    // and multiple immutable borrow of the same place are allowed.
    let px = &p.x;

    println!("{}", px);

    c1();
}