How should we talk about mutability?

The name &mut isn’t really inaccurate or out of place: while it fundamentally means exclusivity, this does always imply mutability as well. If you have an &mut T, that means you get to mutate the T. Given that "being allowed to mutate the T" is usually the thing you care about, as opposed to the exclusivity (which is a means to the end), I think this is a perfectly sensible name.

And the situation is actually slightly more nuanced, because Rust actually does track mutability, itself, as a separate thing in addition to exlusivity. But this is only really done to prevent footguns, and isn’t actually necessary for soundness. (This is what the whole “mutpocalypse” was about last year.) If you have a local variable let foo = ..., then you do have exclusive access to it, but as it’s not marked mut, rustc won’t let you borrow an &mut to it. The situation where you have a let mut foo = ..., borrow &foo, and then also try to borrow &mut foo is different: there you have a genuine shared-vs.-exclusive-access conflict.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.