Closed
Description
This is from a code sample sent to the mailing list: https://mail.mozilla.org/pipermail/rust-dev/2013-November/006924.html
struct Value {
n: int
}
impl Value {
fn squared(mut self) -> Value {
self.n *= self.n;
self
}
}
fn main() {
let x = Value { n: 3 };
let y = x.squared();
println!("{} {}", x.n, y.n);
}
Prints out:
-> % rustc foo.rs && ./foo
9 9
The call to squared
should be receiving a copy of x
as the self param. This bug is only exhibited in the case of implicity copyable struct. If you add something like ~int
to Value the compiler will rightly complain that x has been moved.
Also, if you add another field (like m: int
) making Value an immediate no longer, we see the right behaviour:
-> % rustc foo.rs && ./foo
3 9