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?
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.
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.
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)
© 2022 - 2024 — McMap. All rights reserved.