I am refreshing rust after some time doing other things and it occurred to me that having a table of operations on variables could be useful?
My idea (before learning about interior mutability) was this table below, which I asked a chatbot to generate for me:
Operation
Immutable binding (let)
Mutable binding (let mut)
0) Shadow/re-declare (let x = ...)
Y
Y
1) Reassign (x = ...)
N
Y
2) Update contents (push, insert, etc.)
N
Y
My idea was that, in general, it may be useful to separate what we do with variables as:
Re-Declare as in let a_gain=5; let a_gain=6;
Re-Assign: a_gain += 1 or a_gain=6
Update contents a_gain.push_str("more")
I suspect this is all quite wrong or at least incomplete, and am posting here for some corrections or suggestions, or possibly some extension of the table.
to further your point, i'd like to add that a_gain.push_str("more") can also be written as a += "more", as documented by String in std::string - Rust
but overall, as the table seems to imply, "reassign" and "update contents" are pretty close to being the same thing (well reassigning is a subset of updating contents i would say)
In Rust, thanks to value semantics and the borrow/ownership model, reassignment and updating are very close to being the same thing. In languages with reference/identity semantics, they’re much more distinct, for example in Java assignment of a variable of a class type just updates your local reference to point to another instance, while updating the state of the object is of course visible to everyone with a reference to the object, leading to patterns like "defensive copying" to ensure robustness.