aboutsummaryrefslogtreecommitdiffstats
path: root/tests/compile-fail/mutable.rs
blob: e8885030dc765367f4f3f04d4763fca23897f7c4 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
extern crate clint;

use clint::Handler;

static mut HANDLER: Handler = Handler::new();

fn main() {
    need_move();
    borrow_error();
    no_borrow_needed();
}

fn need_move() {
    let x = vec![1, 2, 3];
    let c = || {
        //~^ ERROR closure may outlive
        println!("x(h-c): {:?}", x);
    };
    unsafe {
        HANDLER.replace(&c);
        HANDLER.call();
        HANDLER.call();
    }
    println!("x(h-o): {:?}", x);
}

fn borrow_error() {
    let x = vec![1, 2, 3];
    let c = move || {
        println!("x(h-c): {:?}", x);
    };
    unsafe {
        HANDLER.replace(&c);
        HANDLER.call();
        HANDLER.call();
    }
    println!("x(h-o): {:?}", x); //~ ERROR borrow of moved value
}

fn no_borrow_needed() {
    let x = vec![1, 2, 3];
    let c = || {
        println!("x(h-c): hi!");
    };
    unsafe {
        HANDLER.replace(&c);
        HANDLER.call();
        HANDLER.call();
    }
    println!("x(h-o): {:?}", x);
}