2019-07-26 16:54:25 -05:00
|
|
|
// run-pass
|
|
|
|
|
2018-09-14 05:20:28 -05:00
|
|
|
#![allow(unused_must_use)]
|
|
|
|
#![allow(dead_code)]
|
2015-03-30 13:00:05 -05:00
|
|
|
use std::thread;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn user(_i: isize) {}
|
2012-05-07 13:31:57 -05:00
|
|
|
|
|
|
|
fn foo() {
|
2014-01-17 16:50:54 -06:00
|
|
|
// Here, i is *copied* into the proc (heap closure).
|
|
|
|
// Requires allocation. The proc's copy is not mutable.
|
2012-05-07 13:31:57 -05:00
|
|
|
let mut i = 0;
|
2015-04-13 17:15:32 -05:00
|
|
|
let t = thread::spawn(move|| {
|
2014-01-17 16:50:54 -06:00
|
|
|
user(i);
|
|
|
|
println!("spawned {}", i)
|
2014-01-27 17:29:50 -06:00
|
|
|
});
|
2014-01-17 16:50:54 -06:00
|
|
|
i += 1;
|
2015-04-13 17:15:32 -05:00
|
|
|
println!("original {}", i);
|
|
|
|
t.join();
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn bar() {
|
2014-01-17 16:50:54 -06:00
|
|
|
// Here, the original i has not been moved, only copied, so is still
|
|
|
|
// mutable outside of the proc.
|
2012-05-07 13:31:57 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 10 {
|
2015-04-13 17:15:32 -05:00
|
|
|
let t = thread::spawn(move|| {
|
2014-01-17 16:50:54 -06:00
|
|
|
user(i);
|
2014-01-27 17:29:50 -06:00
|
|
|
});
|
2012-05-07 13:31:57 -05:00
|
|
|
i += 1;
|
2015-04-13 17:15:32 -05:00
|
|
|
t.join();
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn car() {
|
2014-01-17 16:50:54 -06:00
|
|
|
// Here, i must be shadowed in the proc to be mutable.
|
2012-05-07 13:31:57 -05:00
|
|
|
let mut i = 0;
|
|
|
|
while i < 10 {
|
2015-04-13 17:15:32 -05:00
|
|
|
let t = thread::spawn(move|| {
|
2014-01-17 16:50:54 -06:00
|
|
|
let mut i = i;
|
|
|
|
i += 1;
|
|
|
|
user(i);
|
2014-01-27 17:29:50 -06:00
|
|
|
});
|
2012-05-07 13:31:57 -05:00
|
|
|
i += 1;
|
2015-04-13 17:15:32 -05:00
|
|
|
t.join();
|
2012-05-07 13:31:57 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 16:50:54 -06:00
|
|
|
pub fn main() {}
|