boxed/
boxed.rs

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