137

I learned that if a variable is not explicitly declared mutable using mut, it becomes immutable (it cannot be changed after declaration). Then why do we have the const keyword in Rust? Aren't they the same? If not, how do they differ?

BinaryButterfly
  • 18,137
  • 13
  • 50
  • 91
7_R3X
  • 3,904
  • 4
  • 25
  • 43

7 Answers7

96

const, in Rust, is short for constant and is related to compile-time evaluation. It shows up:

  • when declaring constants: const FOO: usize = 3;
  • when declaring compile-time evaluable functions: const fn foo() -> &'static str

These kinds of values can be used as generic parameters: [u8; FOO]. For now this is limited to array size, but there is talk, plans, and hope to extend it further in the future.

By contrast, a let binding is about a run-time computed value.

Note that despite mut being used because the concept of mutability is well-known, Rust actually lies here. &T and &mut T are about aliasing, not mutability:

  • &T: shared reference
  • &mut T: unique reference

Most notably, some types feature interior mutability and can be mutated via &T (shared references): Cell, RefCell, Mutex, etc.


Note: there is an alternative use of mut and const with raw pointers (*mut T and *const T) which is not discussed here.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • https://doc.rust-lang.org/std/primitive.pointer.html - here is the raw pointer usage It also gets a mention here.. https://doc.rust-lang.org/1.30.0/book/2018-edition/ch19-01-unsafe-rust.html - but that isn't so easy to follow. – JGFMK Aug 29 '20 at 18:17
  • This accepted answer is too complex for newbies like me. Please see below answer by snnsnn for better understanding. – Arun Verma Apr 02 '23 at 19:12
35

const is not for variables; it's for constant values which may not be stored anywhere; they're effectively an alias for a literal value.

Non-mut let declares an actual variable which is created at runtime, can be moved (and no longer accessible), and even have interior mutability (if it contains Cell members, for example) in some cases.

Chris Emerson
  • 13,041
  • 3
  • 44
  • 66
21

A const does not represent a memory location but a value. const values are directly inlined at usage location. Any temporary objects that are created during expression evaluation are accessible only by the compiler during compile time. Can be scoped globally. Can not reference runtime items. Must be type annotated.

Let values represent a memory location. Immutability for let binding is a compiler enforced thing can be changed with mut modifier. It is runtime construct. Always locally scopped. Their types can be inferred by the compiler.

For completeness, a static also represents a memory location like let but any references to the same static are actually a reference to the same memory location. Static are, well, statics. They are compiled into executable and accessible for the entire lifetime of the running program. Can be scoped globally. Can reference other statics. Must be type annotated.

snnsnn
  • 10,486
  • 4
  • 39
  • 44
15

Constants can not be shadowed:

let x = 10u32;
const Y:u32 = 20u32;

let x = 11u32;
//error: duplicate definition of value `Y` [E0428]
//const Y:u32 = 21u32;

println!("x={} Y={}",x,Y); //x=11 Y=20
nbro
  • 15,395
  • 32
  • 113
  • 196
aSpex
  • 4,790
  • 14
  • 25
7

Additionally, we can't make global items using let, but it's possible by using const. Here is an example.

const LENGTH:usize = 4;

fn main() {
    let arr:[i32; LENGTH] = [10,20,30,40];

    for i in 0..LENGTH{
        println!("{}", arr[i])
    }
}

for more information about the usages of const, static, and let: const and static

The story is a little bit longer.

Lillo
  • 71
  • 1
  • 1
3

const is for compile-time constants with everything that entails. For example, you can create a fixed-sized array whose size is a const, but you can't do that with a let binding. Of course, this also means that you can put far, far more things into a let binding than into a const.

1

Conceptually, const items are substituted before parsing, akin to what a C-macro does. This enables it to be used in circumstances where normal variables are not allowed.

const TEN: u32 = 10;
let ten = 10;

// OK
match x {
    TEN => do_a(),
    _ => do_b(),
}

// Nah...
match x {
    ten => do_a(),
    _ => do_b(),
}

// ...use this instead
match x {
    y if x == ten => do_a(),,
    _ => do_b(),
}
Fifnmar
  • 372
  • 1
  • 5
  • 9