Rust variables: immutability by default, mut, and constants
Answer: By default, Rust variables are immutable, but you can opt into mutability with mut, and you can declare always-immutable constants with const.
Immutable variables and mut
When a variable is immutable, once a value is bound to a name, you cannot change that value.
Assigning again to an immutable variable is a compile-time error, such as cannot assign twice to immutable variable x.
The compiler guarantees that a value you marked immutable will not change, which makes the code easier to reason through.
Mutability can still be useful.
You make a variable mutable by adding mut in front of the variable name, which also signals to later readers that other code will change the value.
Constants
Like immutable variables, constants bind a value to a name and are not allowed to change, but they differ in several ways.
You cannot use mut with constants; they are always immutable, not merely immutable by default.
You declare them with the const keyword instead of let, and you must annotate the type.
Constants can be declared in any scope, including the global scope.
They may be set only to a constant expression, not a value that could only be computed at runtime.
The naming convention is all uppercase with underscores between words, as in THREE_HOURS_IN_SECONDS.
Constants remain valid for the entire time a program runs, within the scope where they were declared.
See also
Claims
- id: C1 claim: Rust variables are immutable unless you opt out. source: S1 evidence: "by default, variables are immutable"
- id: C2 claim: Add mut in front of a variable name to make it mutable. source: S2 evidence: "you can make them mutable by adding
mut"
- id: C3 claim: Constants are always immutable; you cannot use mut with them. source: S2 evidence: "you aren't allowed to use
mutwith constants"
Sources
- [S1] Variables and Mutability. The Rust Project Developers. https://doc.rust-lang.org/book/ch03-01-variables-and-mutability.html . License: MIT OR Apache-2.0. Accessed 2026-11-27.
- [S2] Variables and Mutability (source). The Rust Project Developers. https://raw.githubusercontent.com/rust-lang/book/main/src/ch03-01-variables-and-mutability.md . License: MIT OR Apache-2.0. Accessed 2026-11-27.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Variables and Mutability; The Rust Project Developers, Variables and Mutability (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Rust Project Developers, Variables and Mutability; The Rust Project Developers, Variables and Mutability (source). MIT OR Apache-2.0 (https://raw.githubusercontent.com/rust-lang/book/main/LICENSE-MIT). Copyright the Rust Project Developers.
Cite this
iinventedlight. 2026. "Rust variables: immutability by default, mut, and constants." iinventedlight Library, iil:note:0spgg881, version of 2026-11-27. https://iinventedlight.com/library/notes/rust-variables-and-mutability/. License: CC-BY-4.0.
@misc{iil_note_0spgg881,
author = {{iinventedlight}},
title = {{Rust variables: immutability by default, mut, and constants}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-27},
url = {https://iinventedlight.com/library/notes/rust-variables-and-mutability/},
note = {Permanent ID iil:note:0spgg881. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- rust-variables-and-mutability.schema.json (schema; CC-BY-4.0)
- rust-variables-and-mutability.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-variables-and-mutability.md.