What is the difference between immutable and const variables in Rust?
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.
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.
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.