Static mut
static mut is a Rust declaration for mutable statically allocated storage with a program-wide lifetime, whose access requires unsafe handling.
Rust in Action: Systems programming concepts and techniques
static mut declares a mutable Rust static variable with a program-wide lifetime and mutable access requiring unsafe handling.
- How it is used
- Deploys mutable global state for flags, error propagation, and communication with operating-system mechanisms such as signal handlers.
- How deep it goes
- Explains declaration and unsafe reads and writes through complete toy programs, but stops short of detailed concurrency guarantees.
- Examples
- Uses shutdown flags, random Boolean updates, global error codes, file operations, and panic output as concrete demonstrations.
- What you can do after
- Apply mutable statics in small Rust programs that use global flags or error state, while identifying why unsafe access demands caution.
Read how Rust in Action: Systems programming concepts and techniques explains static mut →
Learn Rust in a Month of Lunches
A static mut is a Rust static value that can be directly modified, but accessing it requires unsafe code because unsynchronized access can violate aliasing rules or cause data races.
- How it is used
- Introduces mutable statics as an entry point to unsafe Rust and contrasts them with safer ways to modify shared state.
- How deep it goes
- Demonstrates declaration, compiler rejection, unsafe access, and nondeterministic threaded results, but does not develop a safe replacement.
- Examples
- Uses NUMBER, a ten-thread increment test, and a thousand-thread stress test with inconsistent totals.
- What you can do after
- Diagnose why mutable-static access requires unsafe blocks and why unsynchronized increments can produce undefined behavior.
Read how Learn Rust in a Month of Lunches explains static mut →
Related concepts
- unsafe blockuses · An unsafe block is a Rust code region that permits operations whose safety requirements the compiler cannot verify and whose correctness requires programmer responsibility.
- unsafe Rustuses · Unsafe Rust is Rust code that permits operations whose safety conditions the compiler cannot fully verify and that therefore require the programmer to uphold the relevant invariants.
- static fieldkind of · A named variable whose storage is associated with a class or has static duration, rather than being allocated separately for each object instance or function call.
- global variablekind of · A global variable is a variable whose binding is accessible from broad program or module scope according to the language’s name-resolution rules.
- const qualifiercontrasts with · A language feature that marks a value, binding, or access path as constant, restricting reassignment or modification according to the language’s rules.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

