Rust Reference ยท Keywords and Statements
The static keyword
Reference for Rust static items: global variables with a fixed address, static mut and unsafe, interior mutability, thread safety, and the 'static lifetime.
A static item is a global variable. Unlike const, it occupies exactly one place in memory for the whole run of the program, so every reference to it points to the same address. The initializer is evaluated at compile time and the value is never dropped.
Because anyone can reach a static from any thread, the type of an immutable static must be Sync. To change global state you either use a type with interior mutability that is thread-safe (atomics, Mutex, OnceLock) or a static mut, which requires unsafe for every access. The word also appears in the 'static lifetime, which is a different but related idea.
static VERSION: &str = "1.4.0"; static COUNTER: AtomicUsize = AtomicUsize::new(0);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.
Syntax
static NAME: Type = constant_expression; static mut NAME: Type = constant_expression; let s: &'static str = "text"; fn f<T: 'static>(t: T) { }
| Part | Description |
|---|---|
NAME |
SCREAMING_SNAKE_CASE by convention. |
Type |
Required. For a plain static it must implement Sync. |
mut |
Allows writes but makes every read and write unsafe. |
'static |
A lifetime meaning "valid for the rest of the program"; it is a property of references and types, not only of static items. |
Errors: a static whose type is not Sync is rejected (E0277). Creating a reference to a static mut is a deny-by-default lint in edition 2024 (static_mut_refs); use &raw const and &raw mut instead. Initializers that run code at run time are rejected (E0015).
Usage notes
- Constant versus static: use
constfor values you want inlined andstaticwhen identity matters (an address, a large table you do not want duplicated, or shared mutable state). - Prefer safe designs to
static mut. AnAtomicUsize,Mutex<T>,RwLock<T>orOnceLock<T>gives global state withoutunsafe. - Lazy initialization:
OnceLock(stable since 1.70) andLazyLock(stable since 1.80) compute a value on first use, which fits values that cannot be built in a const context. - Statics are never dropped, so destructors of their contents do not run at exit.
'staticbound does not mean "global".T: 'staticonly says thatTholds no borrowed data shorter than the whole program. An ownedStringsatisfies it. Thread spawning requires it for this reason.- Leaking with
Box::leakproduces a&'static mut Tat run time when you truly need one. - Thread-local alternatives:
thread_local!gives each thread its own copy and avoidsSyncrequirements.