The forums just saw yet another [1] topic about the confusing behavior of the mut
keyword. This was my old pet peeve; Though there have been musings about changing the syntax of either the mutable references or bindings, none have made it out of brainstorming.
The mut
keyword is misleading to beginners because it's overloaded with two, subtly different meanings. let mut
and & mut
looks very similar, so people inevitably assume that they're one and the same feature, when they really aren't.
In another case of overloaded syntax, patterns, there would be a light at the end of the tunnel in which everything will make sense if you study it enough. mut
has no such mercy: it gets worse the more you learn about it.
No one to my knowledge has addressed this problem directly. The suggested changes would've eliminated the overloading, but not as its primary intention.
The suggestion to rename the unique/mutable reference is presumably cancelled due to stability reasons, nothing unusual. The suggestion to remove let mut
on the other hand have met actual oppositions. Note that even if we concede that let mut
is useful, the syntax is still misleading.
If I had my way, I would forbid putting a whitespace between the &
and mut
to give it an intuitive nudge that &mut
is atomic and attempts to understand it as a concatenation of &
and mut
is meaningless.
Lifetime-ascribed reference might be delimited with a different character:
&'a mut Foo
→ &'a:mut Foo
I'm aware none of these syntactic changes are likely to happen. References, even limited to mutable and lifetime-ascribed ones, are common enough that the churn becomes hard to justify, though the ability to soft-deprecate syntaxes between editions helps a bit.
Perhaps we should look out for ways to improve the teachability of &mut
vs. let mut
without changing the language at all. That might be done through lints or learning resources. What kind of plan do you think are viable? I would like to discuss what we can do here.
This comes up again and again. A cursory search on URLO has revealed 7 whole topics on the matter, with more likely to be found. ↩︎