So I'm exploring Rust, and I have read about technical differences between constants and immutable variables. But it seems like immutable variables can do all things that constants can. Then what is the point of existence of constants, if immutable variables can fully substitute them?
-
2Does this answer your question? [What is the difference between immutable and const variables in Rust?](https://stackoverflow.com/questions/37877381/what-is-the-difference-between-immutable-and-const-variables-in-rust) – whilrun Jan 18 '22 at 18:02
-
1@whilrun they only show the difference, but it doesn't make it clear why we need constants at all. Immutable variables can do same things and even more. – Oleksandr Novik Jan 18 '22 at 18:05
-
A valuable comment about memory: https://stackoverflow.com/a/64220569/3054986 – user3054986 Nov 18 '22 at 12:23
-
hah I was asking what the point of immutable variables are when const exists! But yes the same question, I wish the https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html#constants covered this better. – mikemaccana Jul 12 '23 at 11:32
3 Answers
There are two computational times that you should take into account:
- compilation time
- run time
The constant is computed at compilation time (and can be used in other compile-time computation) and hence the run time is faster, as it does need to compute it again.
Immutable variables are always computed at run time (from an external input not available at compilation time usually), and constants cannot be used there.

- 1,340
- 1
- 10
- 18
Then what is the point of existence of constants, if immutable variables can fully substitute them?
While there are certainly use cases in which constants may be interchangeable with immutable variables, the main distinction between the categories of values is their semantics.
Declaring a constant immediately says a lot about what the value is to the reader: in particular, that the information that comprises the value must all be available at compile-time. This is a property which is enforced by the compiler. This sets up expectations for the reader about what the value is and what can be done with it.
Of course, the initialization of immutable variables is much more flexible. There is no mandate that these values are known at compile time, and the calculations that produce such values may be arbitrarily complex and even evolve over time.
The differences are, perhaps, mainly stylistic (in many but not all use cases) but where readability and maintainability are involved the distinction is valuable.

- 9,908
- 3
- 40
- 56
const x
can never be changed. Useful because all constants could be declared in a central place (not distributed over the entire code).let x
(withoutmut
) can be moved (move semantics, i.e. the owner can change). Besides it is possible to (re-)declare it later in the code with anotherlet
orlet mut
(which overwrites the old value of the “immutable” variable).
Therefore a const
has to be used for an array size, because it has to be known for sure at compile time.
Example 1:
const HELLO: &str = "Hello, I feel so solid";
println!("{}", HELLO); // Hello, I fee…
let new_hello= HELLO; // does not move to new_hello, but
// new_hello is a copy of the const
println!("{}", new_hello); // Hello, I fee…
println!("{}", HELLO); // still exists
let bye = "See you later".to_string();
println!("{}", bye); // See you…
let new_bye = bye;
println!("{}", new_bye); // See you…
//println!("{}", bye); // not possible, because content
// of bye has moved to new_bye
// (which is the new owner)
Example 2:
let x = 2; // an immutable variable without `mut`
// attribute
println!("{x}"); // 2
let x = 10; // we declare a new variable with the same
// name x with a new value
// Note: `let x = …` is possible,
// but `x = …` is not!
println!("{x}"); // 10
let mut x = 1000; // again declaration with same name,
// but mutable
println!("{x}"); // 1000
x = -5; // Note: `x = …` is possible
// because of `let mut`
println!("{x}"); // -5 (`x` is mutable now)

- 2,278
- 1
- 23
- 30