# 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:** 1

<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 20, 2018, 3:44am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/1 "2018-04-20T03:44:49Z")

</div>

Hello Everyone,

I propose rust to implement `var` keyword as an alias for `let mut`. Some might think that’s unnecessary and I respect that, however, I think people who would like to create mutable variables by default without much cognitive thinking deserve that option too.

It would also make rust similar to 99% of language out there that has var being the default for mutable variables.

This is similar to the optional keyword `return` found in rust, where you don’t have to use it, but it’s available if you would like to.

The compiler can suggest to turn `var` into `let` if it’s not necessary.

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

It’s doesn’t have to be named `var`, you can pick another to allow the function.

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 but they are implemented differently.

The `var` keyword can be denied by clippy as well if you would like too, that’s in addition to compiler warning in case the variable isn’t muted similar to `let mut` when it’s not necessary to change to `const` or `let`.

Making every Rust variable declaration immutable by default would greatly affect its adoption rate, it greatly reduces productivity in my opinion. I am certain that having both options(let and var) is the best way to go. I am not familiar with any other language that takes this immutable approach. It makes sense to have parameters immutable by default since people almost never mutate parameters; however, this is not the case with variables, and introducing var or similar keyword would be the solution for this concern. This would increase rust adoption rate in my opinion.

Thank you,

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [April 20, 2018, 3:48am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/2 "2018-04-20T03:48:46Z")

</div>

`return` is not an optional keyword - it’s used to return _early_ from a function.

How does writing `var` involve less thinking than `let mut`?

---

<div class="post-metadata">

**Author:** ![ExpHP](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/exphp/32/6208_2.png) [@ExpHP](https://internals.rust-lang.org/u/ExpHP)\
**Post date:** [April 20, 2018, 4:00am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/3 "2018-04-20T04:00:08Z")

</div>

I question the very foundation of this argument; are there _any_ popular languages out there that use `var` other than JavaScript?

> Rust already has a similar situation with the const and let keywords, where both function as an immutable variable but they are implemented differently.

`const` is not an immutable variable. If you try to use `const` for immutable variables, _you're gonna have a bad time._

---

<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 20, 2018, 4:06am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/4 "2018-04-20T04:06:05Z")

</div>

C# has var, Golang has var, and more

---

<div class="post-metadata">

**Author:** ![rpjohnst](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rpjohnst/32/9524_2.png) [@rpjohnst](https://internals.rust-lang.org/u/rpjohnst)\
**Post date:** [April 20, 2018, 4:09am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/5 "2018-04-20T04:09:00Z")

</div>

> [@ExpHP](#):
>
> are there _any_ popular languages out there that use `var` other than JavaScript?

C#, Scala, Go (sort of), Swift, Kotlin, Java 10, probably more.

Not that I think Rust needs `var`- it's hardly a burden when the entire language is designed around immutable-by-default already. The ability to shadow variables in the same scope, iterator-based for loops that handle mutation for you, etc.

---

<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 20, 2018, 4:16am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/6 "2018-04-20T04:16:31Z")

</div>

I believe shadowing variables is a burden as well, I would rather have the option to declare a mutable variable by default in one word using `var` than having the option to mutate my immutable variable by re-declaring it.

---

<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 20, 2018, 5:26am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/7 "2018-04-20T05:26:45Z")

</div>

```
How does writing var involve less thinking than let mut?

```

Because that’s not how it works in 99% of languages you work with. In addition, you require 2 words instead of just one.

---

<div class="post-metadata">

**Author:** ![sfackler](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfackler/32/9_2.png) [@sfackler](https://internals.rust-lang.org/u/sfackler)\
**Post date:** [April 20, 2018, 5:34am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/8 "2018-04-20T05:34:26Z")

</div>

What’s not how it works? C, C++, and Java all have a concept of control over value mutability.

---

<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 20, 2018, 6:37am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/9 "2018-04-20T06:37:53Z")

</div>

I mean that’s not the default mode of variable declaration and cognition in most of languages. In other languages you make a variable and reassign as you like, compilers can catch non mutated variables and tell us to change to constants.

---

<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 20, 2018, 6:50am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/10 "2018-04-20T06:50:31Z")

</div>

Adding `var` was previously discussed [here](https://github.com/rust-lang/rfcs/issues/2304) (closed) from the perspective of JavaScript. I concur with others that we should **not** add `var` and that it will make reading Rust harder.

For the purposes of making mutable bindings easier to deal with, I’ve [proposed](https://github.com/rust-lang/rfcs/pull/2401) allowing `let mut (a, b, ..) = tuple;` as well as `let mut [a, b, c, ..] = arr;`. This is a significantly smaller change and fuses better with the language we already have.

---

<div class="post-metadata">

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

</div>

I don’t think this would only clutter the language, but I prefer the fact you need to type more. It’s kind of syntactic salt ‒ it pushes you into the safer (and easier for compiler to optimise) constant by default. If you introduce `var`, people will forget `let` completely and will be writing only `var`, making the programs more error prone and slower. And they’ll probably disable these millions of warnings about variables not needing to be mutable.

And I _do_ know of languages that go even further. Haskell, which just doesn’t have mutable variables _at all_.

---

<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 20, 2018, 6:56am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/12 "2018-04-20T06:56:49Z")

</div>

> [@vorner](#):
>
> And I do know of languages that go even further. Haskell, which just doesn’t have mutable variables at all.

Uhm, [`IORef`](http://hackage.haskell.org/package/base-4.11.0.0/docs/Data-IORef.html#t:IORef)? 😛

> A mutable variable in the `IO` monad

---

<div class="post-metadata">

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

</div>

> A mutable variable in the IO monad

Well, OK, but it's like saying that Rust has mutable shared state, because it has unsafe cell. It's a library type that breaks the rules, not feature of the language.

---

<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 20, 2018, 7:07am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/14 "2018-04-20T07:07:02Z")

</div>

That’s a good proposal too.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kornel/32/2711_2.png) [@kornel](https://internals.rust-lang.org/u/kornel)\
**Post date:** [April 20, 2018, 11:52am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/15 "2018-04-20T11:52:37Z")

</div>

The need for `var` depends very much on programming style, e.g. golang almost forces to write:

```rust
var x = 1;
if cond {
    x = 2;
}

```

instead of immutable

```rust
let x = if cond {2} else {1};

```

When `const` was added to JavaScript, a lot of people found that it’s possible to use const 99% of time (although it helps that JS’s `const` allows interior mutability).

So IMHO `var` is not that useful if you adopt a more functional programming style.

Whether it should be in Rust depends on whether Rust wants to be flexible and allow other programming styles. But given that Rust is already opinionated towards immutable by default it seems like the decision has been taken in favour of pushing towards functional style.

---

<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 20, 2018, 12:17pm UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/16 "2018-04-20T12:17:21Z")

</div>

I prefer to have `let mut`. It makes it clear that it is mutable.

---

<div class="post-metadata">

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

</div>

Having two equivalent ways to do something is generally bad.

---

<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, 3:39am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/18 "2018-04-21T03:39:55Z")

</div>

Languages should be flexible in order for it to evolve. Let rust community decide by practice which keyword is used. Options are never bad. However, `let mut` with multiple variables is good option as well.

---

<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, 3:49am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/19 "2018-04-21T03:49:22Z")

</div>

If it’s true that `const` is completely different than `let` and compiler can’t do anything about it then I think rust should create another meaningful and expressive keyword such as `final` or `readonly` or something. But I think compiler is possible to emulate `let` using `const` to a great degree based on usage.

---

<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, 6:10am UTC](https://internals.rust-lang.org/t/var-as-alias-for-let-mut/7346/20 "2018-04-21T06:10:43Z")

</div>

What do _you_ think `let` and `const` do? I think that’s where the disconnect is.

Here’s (as I understand it) what they mean to Rust:

`const NAME`: declare a compile-time constant. This is a value that isn’t going to be different between builds; think `const PI: f32 = 3.1415926535;`. Typically used for module level constants, though usable within function contexts–though note that it still means the same thing, thus keeping the all-caps naming convention. The binding does not exist at runtime; it is inlined to its use sites.

`let name`: create a local binding. This is a value that is determined at runtime, and can differ between invocations of the function it is in based on the values of the right side of the assignment. For the most part, this should be your go-to default binding mode when writing functions.

`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.

* * *

In JavaScript it’s `const` or `let` for immutable or mutable bindings. In Java 10 it’s `final var` or `var`. In Rust, it’s `let` or `let mut`.

`const` is a different model, that the compiler has to know the single value of at compile time, and doesn’t exist at runtime, because it’s been inlined. In fact, rustc/LLVM will even do the same thing for `let` bindings and even more complicated expressions if it can fold it down to a constant expression.

There is no need to add `final`/`readonly` – `let` without `mut` already is that.

One last time: `let` and `const` are completely different tools in Rust. `const` in Rust is closest to `static final` in Java or `constexpr` in C++. `let` in Rust is `final var` in Java or `const` in JavaScript. Rust’s `const` is for constants in the mathematical sense: things that have always been and will always be the same.

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