by bcoates · 13y ago · 0 comments
let a: ~Point = ~Point { x: 10, y: 20 };
let b: ~Point = ~Point { x: 20, y: 40 };
if (foo()) {
b = a;
}
println(a.x.to_str()); fn main() {
let mut a = ~1;
let mut b = ~2;
if false { b = a; }
io::println(fmt!("%d", *a));
}
...the compiler will assume that `a` can be moved to `b` (although the precise analysis will reveal it can't) and therefore further use of `a` is unsafe. If you really want to assign `a` to `b`, you can do `b = copy a;` (warning: the syntax may change) to explicitly copy it without moving.The escape hatch is to use the generic "Cell<T>" type in the standard library, which gives you dynamic behavior much like C++11 move semantics. The nice thing about the static analysis, though, is that the checks don't have to be performed at runtime if the compiler can prove that the value is always moved at the end of the block.