2014-01-17 16:50:54 -06:00
|
|
|
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
2012-12-10 19:32:48 -06:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-01-02 01:53:35 -06:00
|
|
|
use std::thread::Thread;
|
2013-05-24 21:35:29 -05:00
|
|
|
|
2012-08-14 14:53:45 -05:00
|
|
|
fn user(_i: int) {}
|
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-01-02 01:53:35 -06: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;
|
|
|
|
println!("original {}", i)
|
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-01-02 01:53:35 -06: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-02 01:53:35 -06: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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-17 16:50:54 -06:00
|
|
|
pub fn main() {}
|