# Mutability and constant what's the difference?

**URL:** <https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691>\
**Category:** uncategorized\
**Created:** [January 26, 2021, 8:42pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691 "2021-01-26T20:42:52Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Henrik41](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/henrik41/32/16251_2.png) [@Henrik41](https://users.rust-lang.org/u/Henrik41)\
**Post date:** [January 26, 2021, 8:42pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/1 "2021-01-26T20:42:53Z")

</div>

So...a variable that cannot change is by definition not a variable!  
Why on earth do we have variables that cannot change by default in Rust.  
What's the difference btw a non mutated variable and a constant?

---

<div class="post-metadata">

**Author:** ![dianegolfouse](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/dianegolfouse/32/18957_2.png) [@dianegolfouse](https://users.rust-lang.org/u/dianegolfouse)\
**Post date:** [January 26, 2021, 8:50pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/2 "2021-01-26T20:50:50Z")

</div>

> [@Henrik41](#):
>
> So...a variable that cannot change is by definition not a variable!

Ha, yes !😁 I think this may be why you often find the term "binding" used in Rust

> [@Henrik41](#):
>
> Why on earth do we have variables that cannot change by default in Rust.

I think it is now considered a best practice for some new languages (I am thinking Rust and, I believe, Swift). This makes mutability opt-in, enabling optimizations for the compiler, and often making the programmer's job easier: immutable ~~variables~~ bindings are easier to reason about, so it makes so sense that they are easier to write 🙂

> [@Henrik41](#):
>
> What's the difference btw a non mutated variable and a constant?

Do you mean `let` vs `const` ?

---

<div class="post-metadata">

**Author:** ![mbrubeck](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/mbrubeck/32/648_2.png) [@mbrubeck](https://users.rust-lang.org/u/mbrubeck)\
**Post date:** [January 26, 2021, 8:51pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/3 "2021-01-26T20:51:38Z")

</div>

The value of a `const` must be known at compile time. An immutable binding may be initialized with a value that is known only at run time.

---

<div class="post-metadata">

**Author:** ![Henrik41](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/henrik41/32/16251_2.png) [@Henrik41](https://users.rust-lang.org/u/Henrik41)\
**Post date:** [January 26, 2021, 8:53pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/4 "2021-01-26T20:53:57Z")

</div>

So it seams that the difference btw a cons and a variable is at compile and run time. Very confusing for someone that comes from a scripting lang.

---

<div class="post-metadata">

**Author:** ![subtle-byte](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/subtle-byte/32/19252_2.png) [@subtle-byte](https://users.rust-lang.org/u/subtle-byte)\
**Post date:** [January 26, 2021, 9:27pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/5 "2021-01-26T21:27:48Z")

</div>

Also

- `const` can be declared in the global scope
- for `const` a type _must_ be annotated

More: [Variables and Mutability - The Rust Programming Language](https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#differences-between-variables-and-constants)

---

<div class="post-metadata">

**Author:** ![trentj](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/trentj/32/5690_2.png) [@trentj](https://users.rust-lang.org/u/trentj)\
**Post date:** [January 26, 2021, 10:58pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/6 "2021-01-26T22:58:11Z")

</div>

```rust
fn print(foo: i32) {
    dbg!(foo);
}

fn main() {
    print(10);
    print(20);
}

```

If I asked you what the value of `foo` is in this program, wouldn't it be correct to say "it varies"?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/scottmcm/32/4286_2.png) [@scottmcm](https://users.rust-lang.org/u/scottmcm)\
**Post date:** [January 26, 2021, 11:56pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/7 "2021-01-26T23:56:40Z")

</div>

> [@dianegolfouse](#):
>
> enabling optimizations for the compiler

Note that `let x =` vs `let mut x =` makes no difference to the optimizer. It's "just a lint", aka you can take any program that works, add `mut`s to all the `let`s, and it'll compile to the same thing. (Other than debug info, column numbers, etc of course.)

> [@dianegolfouse](#):
>
> and often making the programmer's job easier

So rust _could_ have had all the variable mutable by default, but it didn't because we've learned (see both C++ and Javascript, for example) that it's easier for people to understand code if they get a warning up-front that a particular binding is going to get different values in the upcoming code.

> [@Henrik41](#):
>
> What's the difference btw a non mutated variable and a constant?

A `const` in Rust is an item, so it must have its type specified explicitly, cannot reference values or generics from its scope, etc.

---

<div class="post-metadata">

**Author:** ![ZiCog](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/zicog/32/12216_2.png) [@ZiCog](https://users.rust-lang.org/u/ZiCog)\
**Post date:** [January 27, 2021, 4:37am UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/8 "2021-01-27T04:37:51Z")

</div>

> [@Henrik41](#):
>
> Very confusing for someone that comes from a scripting lang.

Depends on the scripting language I guess. Javacsript has `const`:

```rust
const PI = 3.141592653589793;

```

Which you can use instead of `let`or `var`.

Rust us just reversing the default, things are immutable unless stated otherwise.

This is very sensible when used for parameter definitions. Where adding `but`makeis t clear that borrowed items are allowed to be mutated rather than just read.

It's probably better not think in terms of `varaiables` but rather `values`. Unless they really are variable by way of being declared `mut`.

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [January 27, 2021, 3:45pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/9 "2021-01-27T15:45:09Z")

</div>

They can change!

```rust
let x = std::cell::Cell::new(1);
x.set(2);
println!("{}", x.get()); // 2

```

and my [favourite WTF](https://bluss.github.io/rust/fun/2015/10/11/stuff-the-identity-function-does/):

```rust
let v = vec![];
{v}.push(1);

```

The "non-variable" behavior of `let` is only a very shallow lint for people who prefer functional programming style, where single assignment is preferred over reassignment, e.g.:

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

```

can be written as:

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

```

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [April 27, 2021, 3:45pm UTC](https://users.rust-lang.org/t/mutability-and-constant-whats-the-difference/54691/10 "2021-04-27T15:45:42Z")

</div>

Closed on April 27, 2021, 3:45pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
