references/
references.rs

1use std::thread;
2
3fn main() {
4    let mut a = vec![1, 2, 3];
5    let mut x = 0;
6
7    let f1 = &|()| {
8        println!("hello from the first scoped thread");
9        // We can borrow `a` here.
10        dbg!(&a);
11    };
12    let f2 = &mut |()| {
13        println!("hello from the second scoped thread");
14        // We can even mutably borrow `x` here,
15        // because no other threads are using it.
16        x += a[0] + a[2];
17    };
18
19    scope_lock::lock_scope(|e| {
20        thread::spawn({
21            let f = e.fn_(f1);
22            move || f(())
23        });
24        thread::spawn({
25            let mut f = e.fn_mut(f2);
26            move || f(())
27        });
28        println!("hello from the main thread");
29    });
30
31    // After the scope, we can modify and access our variables again:
32    a.push(4);
33    assert_eq!(x, a.len());
34}