# Var as alias for let mut

**URL:** <https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346>\
**Category:** language design\
**Created:** [April 20, 2018, 3:44am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346 "2018-04-20T03:44:49Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Uther](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/uther/32/6225_2.png) [@Uther](https://internals.rust-lang.org/u/Uther)\
**Post date:** [April 21, 2018, 6:24am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/21 "2018-04-21T06:24:17Z")

</div>

Hello,

It seems to me that most of your arguments are not in line with the Rust language design philosophy:

- Rust vision of productivity is not only about typing your code faster but also about having your code correct faster. Rust try to promote the use of immutability whenever possible because it has nice properties about safety. Declaring a mutable variable requires a few more keystrokes on purpose, to prevent from using mutable everywhere by convenience.

- Perl is a famous example of a language that is complex to handle because of too many ways to write the same thing. Rust prefer to prevent two syntax to do the same thing when it is possible to keep the language simple. For instance the ternary operator was removed because the `if` block can do exacty the same. The `return` keyword is necessary to do early return. If early return was not allowed by the language, the return keyword would probably not exist.

- `const` and `let` are very different concepts in Rust. const variable are guarantied to be evaluated at compile time. While let variable are just guarantied to not be modified once there are initialized.

var is a pretty common keyword for variable declaration, but not an ubiquitous one by the way. C++ use auto for instance.

---

<div class="post-metadata">

**Author:** ![parasyte](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/parasyte/32/8925_2.png) [@parasyte](https://internals.rust-lang.org/u/parasyte)\
**Post date:** [April 21, 2018, 6:44am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/22 "2018-04-21T06:44:53Z")

</div>

It should also be pointed out that a lot of the feedback about [productivity in Rust](https://users.rust-lang.org/t/productivity-time-spent-writing-code-vs-test-debug-fix/16508/24) is very positive in Rust’s favor. And the property of immutability by default is one that prevents a lot of unnecessary debug cycles, increasing overall productivity.

---

<div class="post-metadata">

**Author:** ![medozs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/medozs/32/4013_2.png) [@medozs](https://internals.rust-lang.org/u/medozs)\
**Post date:** [April 21, 2018, 6:57am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/23 "2018-04-21T06:57:31Z")

</div>

One last time: Rust should learn from languages such as Java and C# and evolve it’s language and syntax as much as possible. Java and C# are all always evolving and adding feature, these languages aren’t top 5 languages for nothing.

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [April 21, 2018, 7:03am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/24 "2018-04-21T07:03:10Z")

</div>

> [@medozs](#):
>
> Java and C# are all always evolving and adding feature, these languages aren’t top 5 languages for nothing.

I challenge that assertion. To me, Java is an incredibly slow moving language that only adds features when it has been in other languages forever. On the contrary, Rust is to me the language that has an incredible design and implementation pace. I don't think there's any compiler project that is so active as the rustc repository is. Meanwhile, we are accepting new RFCs more or less every other week.

---

<div class="post-metadata">

**Author:** ![medozs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/medozs/32/4013_2.png) [@medozs](https://internals.rust-lang.org/u/medozs)\
**Post date:** [April 21, 2018, 7:05am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/26 "2018-04-21T07:05:44Z")

</div>

That’s great!

---

<div class="post-metadata">

**Author:** ![Centril](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/centril/32/3334_2.png) [@Centril](https://internals.rust-lang.org/u/Centril)\
**Post date:** [April 21, 2018, 7:07am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/27 "2018-04-21T07:07:50Z")

</div>

Orthogonally to the matter of `var`, if we had [scoped type variables](https://wiki.haskell.org/Scoped_type_variables) which are quite nice in Haskell (and for other things), I think `const` inside functions could feel quite close to `let` in spirit; The former being compile time bindings and the latter being runtime bindings. It would be nice seeing some movement in that direction.

> [@CAD97](#):
>
> `let mut name`: create a mutable local binding. This is equivalent to how let works except you are allowed to reassign the binding and to take mutable references to it.

Careful. `let mut` is not the unit! `mut name` is a pattern, and then you can let-bind that pattern with `let mut name`, which you should think of as `let (mut name)`. This is why you have `let (mut foo, mut bar) = pair;` right now.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [April 21, 2018, 9:42pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/28 "2018-04-21T21:42:02Z")

</div>

I was simplifying for comparison’s sake, but you’re right of course.

---

<div class="post-metadata">

**Author:** ![Dylan-DPC](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dylan-dpc/32/3151_2.png) [@Dylan-DPC](https://internals.rust-lang.org/u/Dylan-DPC)\
**Post date:** [April 22, 2018, 2:54pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/29 "2018-04-22T14:54:58Z")

</div>

It isn’t bad. But having multiple ways often confuse people

---

<div class="post-metadata">

**Author:** ![Tom-Phinney](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/tom-phinney/32/3299_2.png) [@Tom-Phinney](https://internals.rust-lang.org/u/Tom-Phinney)\
**Post date:** [April 22, 2018, 3:36pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/30 "2018-04-22T15:36:19Z")

</div>

> [@Dylan-DPC](#):
>
> It isn’t bad. But having multiple ways often confuse people

Actually, I believe that it **is bad** , because it is based on an incorrect parse of the Rust grammar, thus leading to faulty perception. In fact, the title of this topic demonstrates just such an incorrect parse by the topic's originator (and one which I used to make myself 🙄).

As @Centril pointed out

> [@Centril](#):
>
> `let mut` is not the unit! `mut name` is a pattern, and then you can let-bind that pattern with `let mut name`, which you should think of as `let (mut name)`.

The lesson I've learned from this discussion is that Rust's documentation should emphasize @Centril's point: that `let` is the declaration and `mut`, if present, is an attribute of the identifier being declared. Per @Centril's example, slightly modified, it is possible to write

```rust
let (foo, mut bar) = pair;

```

which clearly demonstrates that the proposed `var` syntax is **not** an alias.

---

<div class="post-metadata">

**Author:** ![Dylan-DPC](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dylan-dpc/32/3151_2.png) [@Dylan-DPC](https://internals.rust-lang.org/u/Dylan-DPC)\
**Post date:** [April 22, 2018, 3:40pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/31 "2018-04-22T15:40:11Z")

</div>

> [@Tom-Phinney](#):
>
> Actually, I believe that it is bad, because it is based on an incorrect parse of the Rust grammar, thus leading to faulty perception

Me too. But I didn't want to sound too negative 😁

---

<div class="post-metadata">

**Author:** ![notriddle](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/notriddle/32/14082_2.png) [@notriddle](https://internals.rust-lang.org/u/notriddle)\
**Post date:** [April 22, 2018, 11:27pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/32 "2018-04-22T23:27:26Z")

</div>

> [@medozs](#):
>
> Options are never bad.

Yeah, they sometimes are.

> **[Decision fatigue](https://en.wikipedia.org/wiki/Decision_fatigue)**
>
> In decision making and psychology, decision fatigue refers to the deteriorating quality of decisions made by an individual after a long session of decision making. It is now understood as one of the causes of irrational trade-offs in decision making. Decision fatigue may also lead to consumers making poor choices with their purchases.
> There is a paradox in that "people who lack choices seem to want them and often will
> fight for them", yet at the same time, "people find that making many choices c...

---

<div class="post-metadata">

**Author:** ![mjj](https://avatars.discourse-cdn.com/v4/letter/m/b487fb/32.png) [@mjj](https://internals.rust-lang.org/u/mjj)\
**Post date:** [April 24, 2018, 3:27pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/33 "2018-04-24T15:27:13Z")

</div>

you may also like this reference along the same lines:

> Monotony of design - there should be only one way to accomplish a certain atomic task in an application [or language in this case]

from: [The Humane Interface - Wikipedia](https://en.wikipedia.org/wiki/The_Humane_Interface)

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [April 25, 2018, 6:32am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/34 "2018-04-25T06:32:15Z")

</div>

I strongly oppose this request because it would only clutter the language without any benefits; actually, it would be a step backwards in terms of reliability and correctness. However, it also seems to me that you suffer from a serious lack of understanding of Rust's syntax, semantics, and overall goals/roadmap. So let me address that first and break down what's wrong with your arguments:

> `var` keyword as an alias for `let mut`

`let mut` is not _one_ keyword/syntactic construct, but two. It's a `let` binding with a `mut` pattern. In other words, it's a composition of two syntactic elements. You can do `let Foo { field_1, mut field2 } = some_struct;` in a struct, for example, or `if let Some(value) = an_optional { … }`. Thus, **it doesn't make sense to talk about "an alias of `let mut`",** because you can't alias it, **it's simply not a thing.** In other words, `var` wouldn't compose, it would be a very niche addition for one particular constellation of `let` and `mut`, which would work in only one case: when the `let` and `mut` tokens happen to follow each other. This would be a serious violation of the decomposability of Rust's concrete syntax into a tree, because it is no longer about syntactic **structure,** but approximately plaintext search-and-replace.

> I think people who would like to create mutable variables by default without much cognitive thinking deserve that option too.

People who would like to create mutable variables by default are wrong. The last 30 years of the history of computer science and software engineering proved that immutability is one of the best and easiest ways to achieve reliability, and that for this reason, immutable-by-default is the right choice when designing a programming language. I'm not a purist or an extremist: I don't like pure functional languages that don't allow mutable variables _at all_. Sometimes, they are necessary and a better solution than trying to enforce immutability in 100% of the time. However, they shouldn't be the _default_, simply because programs written in a mostly immutable/mostly functional style are much easier to understand and read/write correctly.

Furthermore, I simply can't believe that having to type `let mut` is so much of a cognitive overhead compared to `var` that a non-composable, special-cased, new keyword would be worth introducing to the language.

I have a feeling that you haven't been using Rust for a long time. Some Rust newcomers ask for features of languages they have been using, because they don't like Rust's explicitness at first. For example, some people complain that optionals have to be written using an explicit `Some(…)`, but when they gain a bit of experience with Rust, they understand why it is a better way to write optionals. I think you might be in a similar situation with respect to mutability.

> It would make rust more natural as a programming language similar to most languages.

Again, it's such a minor similarity or difference that it's not worth the burden of a change to the core language. `let mut` is not anything weird or hard to understand in particular. Of course, Rust tries to not be super weird or unfamiliar, which is a good thing. However, if the primary goal of a language was to be as similar to other languages as possible, that language might as well just not exist at all, and programmers could use one of the other language it was supposed to be similar to. Rust is not a language that has similarity to JavaScript, C#, Go, or Swift among its primary goals. (Especially in the light of two of those languages being younger than Rust.)

> Making every Rust variable declaration immutable by default would greatly affect its adoption rate, it greatly reduces productivity.

No, it doesn't. Rust users are among the most productive programmers in the world, and people [love it the most, as it is](https://insights.stackoverflow.com/survey/2018/#technology-most-loved-dreaded-and-wanted-languages).

> You might say that having `let` and `var` would make it confusing; however, Rust already has a similar situation with the `const` and `let` keywords, where both function as an immutable variable.

No, they don't. Neither of these two constructs are doing what you think they are doing. `const` is a compile-time constant, meaning that its value is known to the compiler. `const` is not used for immutable variables. `let` is also not used for immutable variables only. `let` is used for binding names to values, where the presence or absence of `mut` before a certain name in a pattern shows if that particular name will be a mutable binding.

Alright, so now that I hopefully cleared up some confusion, let's address the last bad part of `var`: it would be another way of writing exactly the same thing for one particular construct. Having two pieces of syntax for exactly the same semantics is _very_ confusing. It doesn't only lack a significant advantage, it's actually **actively harmful.** because our monkey brains work like that. If two things look different, we expect that they be different. Similarly, if two things look the same, we expect them to behave similarly. Breaking this intuition is a very dangerous game, because it will lead to confusion, which in turn leads to bugs. It's better to spare ourselves that pain.

---

<div class="post-metadata">

**Author:** ![medozs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/medozs/32/4013_2.png) [@medozs](https://internals.rust-lang.org/u/medozs)\
**Post date:** [April 25, 2018, 3:18pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/35 "2018-04-25T15:18:49Z")

</div>

It seems that rust is a perfect language that needs no evolution or modification. Thank You guys for your inputs. Sorry for my request. I promise I won’t create any requests or discussions again.

---

<div class="post-metadata">

**Author:** ![medozs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/medozs/32/4013_2.png) [@medozs](https://internals.rust-lang.org/u/medozs)\
**Post date:** [April 25, 2018, 3:43pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/36 "2018-04-25T15:43:04Z")

</div>

You have your opinion and style of programming and I have mine. You can’t force or degrade the opinions of others. It’s not a dictatorship here.

---

<div class="post-metadata">

**Author:** ![goodmanjonathan](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/goodmanjonathan/32/3113_2.png) [@goodmanjonathan](https://internals.rust-lang.org/u/goodmanjonathan)\
**Post date:** [April 25, 2018, 3:44pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/37 "2018-04-25T15:44:02Z")

</div>

Please continue to be involved! The language is not perfect and never will be. Discussions like this are the best way to drive improvements.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [April 25, 2018, 3:46pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/38 "2018-04-25T15:46:39Z")

</div>

I didn’t force any particular style on anyone. Explaining why one style is factually better than another isn’t “forcing” – I didn’t point a gun at your head. Your reasoning and arguments, however, showed clear evidence of lack of experience; I attempted to explain to you why/how it is a fact that immutability helps avoid bugs. If you don’t want to program in that style, that’s up to you. However, you can’t expect the language to change in that direction either.

---

<div class="post-metadata">

**Author:** ![medozs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/medozs/32/4013_2.png) [@medozs](https://internals.rust-lang.org/u/medozs)\
**Post date:** [April 25, 2018, 3:57pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/39 "2018-04-25T15:57:22Z")

</div>

You clearly didn’t force anything on me. You just dictated how a language should and must be and degraded other opinions as invalid and you clearly haven’t forced anything.

---

<div class="post-metadata">

**Author:** ![medozs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/medozs/32/4013_2.png) [@medozs](https://internals.rust-lang.org/u/medozs)\
**Post date:** [April 25, 2018, 4:14pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/42 "2018-04-25T16:14:17Z")

</div>

This should be a place of respectful discussion, not degrading opinions and insulting others. If you don’t like the idea, should leave a respectful comment. It’s just discussion after all, no one will reinventing rust in a day. It’s just a discussion for the record.

Also, when something was already said by other members, you don’t need to repeat it again just for the sake of creating a post of your own. Most of your arguments were already said before. If you want to add something, add something new.

---

<div class="post-metadata">

**Author:** ![medozs](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/medozs/32/4013_2.png) [@medozs](https://internals.rust-lang.org/u/medozs)\
**Post date:** [April 25, 2018, 4:29pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/43 "2018-04-25T16:29:12Z")

</div>

Thank You guys for your inputs, you all have valid arguments. Hopefully this discussion will remain a reference guide for the future. I don’t actually think `var` is needed at this point. Any problems with writing preference or style can be solved through IDE, not requiring language change.

[Previous page](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346.md?page=1)

[Next page](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346.md?page=3)
