Can we always use let mut to define variables?

Maybe this is a stupid question, but I want to discuss it with you. I've been learning Rust for more than one year, and I'm also a Pythoner, Gopher, this often makes me forget the difference between these languages, e.g. I often forget to write mut, my questions are:

Can I just always use let mut to define variables?
Can I use auto to alias let mut?

Any help would be greatly appreciated.

You cannot use auto to alias let mut. You can always use let mut to define variables, but it is bad practice. Instead think about which variables you need to mutate and only mark those, or if you don't want to think about it, just use let to define your variables and change it to let mut when the compiler complains about it (after checking that it is indeed supposed to be mutated).

This ensures that you only mutate values that are supposed to be mutated, and helps prevent silly typo bugs where you accidentally use another variable.


:thinking:, thanks for your explanation.

mut is not paired with let in syntax, it's paired with each variable declaration. Think about the case below.

let (a, mut b) = get_pair();

Yeah, :thinking:

This might not be the help you were looking for, but this lint has saved me from quite some bugs in my code. So my advice would be to just use let without mut all the time, and let the compiler guide you where to add a mut, and just doing a quick sanity check in your head while doing so. The ratio of "time invested" over "bugs averted" is pretty good here, at least for me.


Unlike most other languages, Rust has a very strong emphasis on correctness and maintainability.

Basically, that means making it easy to avoid bugs, and making it easy to understand the code, and making it easy to refactor the code.

Because mutability is a major source of bugs, Rust tries to prevent unnecessary mutability.

That also aids with refactoring, because you know whether something is mutated or not.

This can also improve performance, because if the Rust compiler knows that something won't be mutated, it can do some extra optimizations on it.

Rust takes the viewpoint that programs are read and run much more often than they are written, so it's better for the programmer to spend a little extra effort (adding mut) if it benefits people who are reading the code, or who are running the program.

Also, I would like to mention that because Rust has such a strong emphasis on correctness, the compiler is very good at catching mistakes (much more so than most other languages).

So it's very common to just write Rust code in a fairly loose style, and then fix up any compiler errors afterwards.

This removes a lot of stress from programming, because you no longer need to worry about many things: the compiler will catch it!

So don't worry about making your program perfect right away, instead take a more exploratory approach. Rely on the compiler, trust the compiler, it is your friend!


That's kind of a half-truth. The let mut variable declarations don't help the compiler optimize anything. It already can tell when local variables are being mutated, because it always analyzes the entire function body at once to do type inference. That stuff is really just a lint.

What helps the type system and optimizer are the explicit &mut references. That's because the compiler can't always match the spot where the variables are declared with the spot where the reference gets written to, because references add a layer of indirection.


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