I'm not sure whether this has been raised before. In the past i had suggested that missing mut
in patterns could be downgraded to a deny-by-default lint. Over time i have changed my mind, now here's my recent thinking:
We could have a postfix mut
block, evaluating to ()
as result, that temporarily make a place (like local variables, etc) mutable. (Syntax subject to bikeshedding)
Example:
let ds = DataStructure::new();
ds.mut {
// this set up internal state within `ds`,
// without using `OnceLock` and friends.
ds.initialize_with(&env);
};
ds.query_a();
ds.query_b();
This allows much more fine-grained mutable scopes, without breaking the intention that ds should be shared but not mutable during the following queries. I believe this is much more inline with Rust's original design, than "missing mut
in patterns be downgraded to a deny-by-default lint" or something similar.
Of course today this effect is also achievable with:
let ds = {
let mut ds = DataStructure::new();
ds.initialize_with(&env);
ds
};
ds.query_a();
ds.query_b();
But this requires initialize_with
be called immediately, which is a lot less flexible.