rust/tests/ui/borrowck/kindck-implicit-close-over-mut-var.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

50 lines
966 B
Rust
Raw Normal View History

//@ run-pass
#![allow(unused_must_use)]
#![allow(dead_code)]
use std::thread;
fn user(_i: isize) {}
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.
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();
}
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.
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
});
i += 1;
2015-04-13 22:15:32 +00:00
t.join();
}
}
fn car() {
2014-01-17 22:50:54 +00:00
// Here, i must be shadowed in the proc to be mutable.
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
});
i += 1;
2015-04-13 22:15:32 +00:00
t.join();
}
}
2014-01-17 22:50:54 +00:00
pub fn main() {}