# Suggestion: "mut", like "let" keyword

**URL:** <https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851>\
**Category:** language design\
**Created:** [November 17, 2018, 1:18am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851 "2018-11-17T01:18:55Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kroc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kroc/32/1788_2.png) [@Kroc](https://internals.rust-lang.org/u/Kroc)\
**Post date:** [November 17, 2018, 1:18am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/1 "2018-11-17T01:18:55Z")

</div>

I would like to propose a `mut` keyword that simply provides visual balance to the `let` keyword.

E.g.

```rust
let x = 6
mut thing.value = x

```

The idea is purely to provide visual balance and make it clear when something mutable occurs; this should act as a reminder to new users that they need to define a variable as mutable in order to mutate it.

It also helps to make it clearer when you’re about to make the mistake of trying to modify a non-mutable reference.

---

<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:** [November 17, 2018, 1:54am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/2 "2018-11-17T01:54:21Z")

</div>

“mut” is an attribute that occurs in NINE different productions of the Rust grammar. It is not a verb. I’m sure some of the Rust language team can explain this further.

---

<div class="post-metadata">

**Author:** ![mark-i-m](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mark-i-m/32/3167_2.png) [@mark-i-m](https://internals.rust-lang.org/u/mark-i-m)\
**Post date:** [November 17, 2018, 4:06am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/3 "2018-11-17T04:06:41Z")

</div>

Also, I feel like this would cause people to be more likely to use `mut` by default, which IMHO would be bad.

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [November 17, 2018, 9:47am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/5 "2018-11-17T09:47:33Z")

</div>

They are suggesting an optional “mut” at the start of an arbitrary assignment - not bindings.

E.g.

```rust
let foo = 1;
let bar = || foo;
mut foo = 2;
assert!(bar() == 2);

```

---

<div class="post-metadata">

**Author:** ![Kroc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kroc/32/1788_2.png) [@Kroc](https://internals.rust-lang.org/u/Kroc)\
**Post date:** [November 17, 2018, 9:53am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/6 "2018-11-17T09:53:32Z")

</div>

Correct, this is not about bindings. I am not aware of the details of the whole language so I can’t say if this wouldn’t fall apart in practice, but just as the “dyn” keyword has been forced on Trait objects, purely for a heads-up warning to the developer “WARNING: THIS ISN’T STATIC!”, I’d like a “mut” keyword so that as you scan the code you can see “modification happens here”.

If new users do take to writing it by default, then it only helps when the compiler spits out the warning “you are trying to mutate a thing here, but it’s not defined as mutable --\> here”.

IMO

```rust
let x = 6
mut thing.value = x

```

looks better than

```rust
let x = 6
thing.value = x

```

especially when scanning code quickly, it allows you to identify the assignments out from function calls etc.

---

<div class="post-metadata">

**Author:** ![ahmedcharles](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ahmedcharles/32/4041_2.png) [@ahmedcharles](https://internals.rust-lang.org/u/ahmedcharles)\
**Post date:** [November 17, 2018, 10:00am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/7 "2018-11-17T10:00:34Z")

</div>

That’s just the same as looking for every instance of `=`, right?

---

<div class="post-metadata">

**Author:** ![Soni](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@Soni](https://internals.rust-lang.org/u/Soni)\
**Post date:** [November 17, 2018, 3:56pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/8 "2018-11-17T15:56:47Z")

</div>

how about:

```rust
let foo;

foo = bar;

```

---

<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:** [November 17, 2018, 5:14pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/9 "2018-11-17T17:14:41Z")

</div>

In the Rust grammar, `let` occurs in `condition` sub-expressions within `if` and `while` expressions, as well as in one of the four alternative productions for statements.

In my experience, relatively few `=` assignment statements are instances of delayed initialization of immutable variables. If that’s also the case for the OP, they could handle those cases by annotating _only_ those assignment statements:

```rust
let foo;
\*!mut*\ foo = bar;

```

All other assignment statements [**added** : without a `let` prefix] in the program would be presumed to be assignments or **re** assignments to `mut` variables.

For the record, here are the current instances of the “mut” keyword in the productions of the draft grammar for Rust that is being developed by the `grammar-wg`. (In the following, `…` means that the production contains other alternatives that are not shown.)

```rust
Expr = attrs:OuterAttr* kind:ExprKind;
ExprKind = …
    | Borrow:{ "&" mutable:"mut"? expr:Expr }

Item = attrs:OuterAttr* vis:Vis? kind:ItemKind;
ItemKind = …
    | Static:{ "static" mutable:"mut"? name:IDENT ":" ty:Type "=" value:Expr ";" }

ForeignItem = attrs:OuterAttr* vis:Vis? kind:ForeignItemKind;
ForeignItemKind = …
    | Static:{ "static" mutable:"mut"? name:IDENT ":" ty:Type ";" }

FnArg = …
    | SelfValue:{ mutable:"mut"? "self" }
    | SelfRef:{ "&" lt:LIFETIME? mutable:"mut"? "self" }

Pat = …
    | Ref:{ "&" mutable:"mut"? pat:Pat }

Binding = boxed:"box"? reference:"ref"? mutable:"mut"? name:IDENT;

Type = …
    | RawPtr:{ "*" { "const" | mutable:"mut" } pointee:Type }
    | Ref:{ "&" lt:LIFETIME? mutable:"mut"? pointee:Type }

```

---

<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:** [November 17, 2018, 5:59pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/10 "2018-11-17T17:59:35Z")

</div>

```rust
let x = 6;
{thing}.value = x;

```

This [works already](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=3a0da15eeafcf6992a8d37171efe7f10) in Rust today. Problem solved 😉

---

<div class="post-metadata">

**Author:** ![Kroc](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kroc/32/1788_2.png) [@Kroc](https://internals.rust-lang.org/u/Kroc)\
**Post date:** [November 17, 2018, 6:23pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/11 "2018-11-17T18:23:54Z")

</div>

How curious :S To those saying “you can already do this”; that’s like saying that `try!` already exists, there was no need for `?`.

That’s not helping.

Also, the reason why an RFC of this would likely die on the first comment 😕

---

<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:** [November 17, 2018, 6:40pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/12 "2018-11-17T18:40:45Z")

</div>

The cognitive dissonance here is that `let` is a keyword that results in binding a type and/or an evaluated expression to an identifier. `mut` is simply an attribute of that binding, conceptually no different than `&` or `static` or `const` or `ref` or a lifetime `'a`.

IIUC, the OP’s proposal is to prefix that one attribute to assignment statements when the target of the assignment has that attribute. Should the same promotion of attributes to the beginning of statements occur for other attributes? Would it make sense to prefix assignment statements for references with `&`?

---

<div class="post-metadata">

**Author:** ![OZ\_](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oz_/32/4298_2.png) [@OZ\_](https://internals.rust-lang.org/u/OZ_)\
**Post date:** [November 18, 2018, 7:23am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/13 "2018-11-18T07:23:09Z")

</div>

It works quite [strange](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=3a0da15eeafcf6992a8d37171efe7f10) Please share some URL where I can read about this `{variable}.key` syntax - can’t find it using google.

---

<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:** [November 18, 2018, 7:41am UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/14 "2018-11-18T07:41:24Z")

</div>

This is just a normal block `{ /* code */ }`, so `{thing}` moves `thing`, then `.value` accesses the place on that temporary, which is then `= x` assigned and `;` then tossed.

[Using `thing` afterwards](https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=87586098e5f9e990ea7ddb4e41840c7f) will show that it’s been moved.

You’ll somewhat rarely see `{x}` used to enforce a move on `x` rather than treating it as a place. (Personally, I typically end up creating a `fn coerce<T>(t: T) -> { t }` that I use to enforce moves. It’s nice for being able to write e.g. `coerce::<&str>(&String)` as well since type ascription in expressions isn’t anywhere near stable yet.)

---

<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:** [November 18, 2018, 2:02pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/15 "2018-11-18T14:02:33Z")

</div>

Oh, right. I missed that.

---

<div class="post-metadata">

**Author:** ![OZ\_](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oz_/32/4298_2.png) [@OZ\_](https://internals.rust-lang.org/u/OZ_)\
**Post date:** [November 18, 2018, 2:15pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/16 "2018-11-18T14:15:13Z")

</div>

> [@CAD97](#):
>
> then `.value` accesses the place on that temporary

Sorry, I can't understand this part yet. What exactly we access if braces here are just normal block? Right now for me it's the same as write `{ let x = 5; println!("{}", x); }.some_field` - what is the difference?

---

<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:** [November 18, 2018, 2:18pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/17 "2018-11-18T14:18:50Z")

</div>

Nothing. It’s literally just a block. The value of the block is the value of the final expression. So if the block only has one expression, then that is the value. Therefore the value of `{x}` is just the value of `x` itself.

---

<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:** [November 18, 2018, 2:29pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/18 "2018-11-18T14:29:42Z")

</div>

> [@OZ\_](#):
>
> Sorry, I can’t understand this part yet. What exactly we access if braces here are just normal block? Right now for me it’s the same as write `{ let x = 5; println!("{}", x); }.some_field` - what is the difference?

The difference between `x.value =` and `{x}.value =` is that `x` is a place expression whereas `{x}` is a value expression. See [https://doc.rust-lang.org/1.30.0/reference/expressions.html#place-expressions-and-value-expressions](https://doc.rust-lang.org/1.30.0/reference/expressions.html#place-expressions-and-value-expressions) for a detailed explanation.

---

<div class="post-metadata">

**Author:** ![OZ\_](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oz_/32/4298_2.png) [@OZ\_](https://internals.rust-lang.org/u/OZ_)\
**Post date:** [November 18, 2018, 2:41pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/19 "2018-11-18T14:41:49Z")

</div>

thanks, now I get it

---

<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:** [November 18, 2018, 2:50pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/20 "2018-11-18T14:50:01Z")

</div>

```rust
let mut k;
loop {
    k = fetch_data(); // should this get a `mut` prefix?
    if good(k) { break };
}

```

---

<div class="post-metadata">

**Author:** ![160R](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/160r/32/9502_2.png) [@160R](https://internals.rust-lang.org/u/160R)\
**Post date:** [November 18, 2018, 7:07pm UTC](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851/21 "2018-11-18T19:07:04Z")

</div>

I’d rather use something like “mutable accessor” for mutating fields:

```rust
let x = 6;
thing~value = x;

```

And for calling functions which takes `&mut self`:

```rust
thing
    ~mutate()
    ~mutate_again()
    .move_it();

```

If you interested, more use cases and syntax variations could be found in [this thread](https://internals.rust-lang.org/t/method-cascading-and-pipe-forward-operators-proposal/7384).

[Next page](https://internals.rust-lang.org/t/suggestion-mut-like-let-keyword/8851.md?page=2)
