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