File: borrowck-multiple-captures.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (61 lines) | stat: -rw-r--r-- 1,347 bytes parent folder | download | duplicates (11)
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
use std::thread;


fn borrow<T>(_: &T) { }


fn different_vars_after_borrows() {
    let x1: Box<_> = Box::new(1);
    let p1 = &x1;
    let x2: Box<_> = Box::new(2);
    let p2 = &x2;
    thread::spawn(move|| {
        //~^ ERROR cannot move out of `x1` because it is borrowed
        //~| ERROR cannot move out of `x2` because it is borrowed
        drop(x1);
        drop(x2);
    });
    borrow(&*p1);
    borrow(&*p2);
}

fn different_vars_after_moves() {
    let x1: Box<_> = Box::new(1);
    drop(x1);
    let x2: Box<_> = Box::new(2);
    drop(x2);
    thread::spawn(move|| {
        //~^ ERROR use of moved value: `x1`
        //~| ERROR use of moved value: `x2`
        drop(x1);
        drop(x2);
    });
}

fn same_var_after_borrow() {
    let x: Box<_> = Box::new(1);
    let p = &x;
    thread::spawn(move|| {
        //~^ ERROR cannot move out of `x` because it is borrowed
        drop(x);
        drop(x); //~ ERROR use of moved value: `x`
    });
    borrow(&*p);
}

fn same_var_after_move() {
    let x: Box<_> = Box::new(1);
    drop(x);
    thread::spawn(move|| {
        //~^ ERROR use of moved value: `x`
        drop(x);
        drop(x); //~ ERROR use of moved value: `x`
    });
}

fn main() {
    different_vars_after_borrows();
    different_vars_after_moves();
    same_var_after_borrow();
    same_var_after_move();
}