10 ms·
I think this is a good change. Consider a program like this [one][1]: fn mutter( x : ~int ) -> ~int { let mut y = x; *y = 24; retu
by Tyr42 12y ago
I think this is a good change.
Consider a program like this [one][1]:
fn mutter( x : ~int ) -> ~int {
let mut y = x;
*y = 24;
return y;
}
fn main() {
let y = box 42;
let x = mutter( y );
println!("x:{}", x);
}
That's currently legal, but we mutate the box with 42 in it
in a function that doesn't declare itself mut, because it has the only reference to the box, it can freely start mutating it, and then give it back at the end.
[1]: https://gist.github.com/tbelaire/d5d6950cb0798cf6e194 https://gist.github.com/tbelaire/d5d6950cb0798cf6e194
- azth 12y agoInteresting. However, in main(), the variable `y` can no longer be referenced, since it would fail the borrow checker. Doesn't that effectively mean that it is immutable, as it won't be referenced any longer?
- xiaq 12y agoI would say it's a proof that mutability is overcomplicated if you have to define it in such ways...