mozilla/rust — Issue #1894: Last uses aren't moved into closures by default
Author: brson
Created: 2012-02-23 22:50:27+00:00
State: open

This fails:

```
fn main() {
let x = ~1;
let x_in_parent = ptr::addr_of(*x) as uint;
let f = fn@() -> uint {
ptr::addr_of(*x) as uint
};
let x_in_child = f();
assert x_in_parent == x_in_child;
}
```

Rewriting it with a capture clause works:

```
fn main() {
let x = ~1;
let x_in_parent = ptr::addr_of(*x) as uint;
let f = fn@[move x]() -> uint {
ptr::addr_of(*x) as uint
};
let x_in_child = f();
assert x_in_parent == x_in_child;
}
```

This is a bit surprising and makes spawn not do the expected thing when written with the convenient block closure syntax. For this reason core::task uses `fn~` closures with explicit capture clauses.