can borrow mut in proc Fixes #10617
This commit is contained in:
parent
4176343073
commit
ea9db66c50
2 changed files with 18 additions and 35 deletions
|
|
@ -1,4 +1,4 @@
|
|||
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
||||
// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
|
|
@ -13,38 +13,41 @@ use std::task;
|
|||
fn user(_i: int) {}
|
||||
|
||||
fn foo() {
|
||||
// Here, i is *moved* into the closure: Not actually OK
|
||||
// Here, i is *copied* into the proc (heap closure).
|
||||
// Requires allocation. The proc's copy is not mutable.
|
||||
let mut i = 0;
|
||||
do task::spawn {
|
||||
user(i); //~ ERROR mutable variables cannot be implicitly captured
|
||||
user(i);
|
||||
println!("spawned {}", i)
|
||||
}
|
||||
i += 1;
|
||||
println!("original {}", i)
|
||||
}
|
||||
|
||||
fn bar() {
|
||||
// Here, i would be implicitly *copied* but it
|
||||
// is mutable: bad
|
||||
// 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 {
|
||||
do task::spawn {
|
||||
user(i); //~ ERROR mutable variables cannot be implicitly captured
|
||||
user(i);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn car() {
|
||||
// Here, i is mutable, but *explicitly* shadowed copied:
|
||||
// Here, i must be shadowed in the proc to be mutable.
|
||||
let mut i = 0;
|
||||
while i < 10 {
|
||||
{
|
||||
let i = i;
|
||||
do task::spawn {
|
||||
user(i);
|
||||
}
|
||||
do task::spawn {
|
||||
let mut i = i;
|
||||
i += 1;
|
||||
user(i);
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
}
|
||||
pub fn main() {}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue