← All notes

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

Sources

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

Permanent ID: iil:note:0spgg881

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.},
}

Markdown

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.

All artifact files (JSON index)

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.