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