reference icon indicating copy to clipboard operation
reference copied to clipboard

Document what `mut` does in identifier patterns

Open mheiber opened this issue 3 years ago • 1 comments

The syntax is documented in the reference: https://doc.rust-lang.org/stable/reference/patterns.html#identifier-pattern, but afaict not the meaning.

Impact

Experienced users can still get tripped up by things like: https://stackoverflow.com/questions/69845063/why-does-a-level-of-indirection-allow-omitting-mut (I'm a Rust n00b, but posted the question for a Rust-experienced colleague).

How to fix:

If someone has a definition of mut handy, I can make PRs to update relevant parts of the docs. Apologies if mut is already defined and I missed it.

moved from https://github.com/rust-lang/rust/issues/90605

mheiber avatar Nov 05 '21 17:11 mheiber

how about

mut ident means you can create mutable references to ident that can last as long as ident, which you cannot do with immutable bindings. You can also assign to a variable that is mutable to change its value. This is simliar to shadowing, except you cannot assign it to a differently typed expression, which you can with shadowing. Also, a shadowed variable "retains" its previous type and value. After it goes out of scope, it "returns" to its previous value and type. While assigning to a mutable variable changes its value for the rest of its lifetime.

pro465 avatar Jan 19 '22 07:01 pro465