Mutability and constant what's the difference?

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?

Ha, yes !:grin: I think this may be why you often find the term "binding" used 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 :slightly_smiling_face:

Do you mean let vs const ?

3 Likes

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.

4 Likes

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.

Also

  • const can be declared in the global scope
  • for const a type must be annotated

More: Variables and Mutability - The Rust Programming Language

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"?

4 Likes

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 muts to all the lets, and it'll compile to the same thing. (Other than debug info, column numbers, etc of course.)

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.

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

1 Like

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

const PI = 3.141592653589793;

Which you can use instead of letor var.

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

This is very sensible when used for parameter definitions. Where adding butmakeis 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.

They can change!

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

and my favourite WTF:

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

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

can be written as:

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

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.