Skip to content
Tutorials Center
My learning

Topics

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.

Rust
static VERSION: &str = "1.4.0";
static COUNTER: AtomicUsize = AtomicUsize::new(0);

Try it

Rust Immutable statics and atomics

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.

Rust static mut needs unsafe

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

Rust
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 const for values you want inlined and static when identity matters (an address, a large table you do not want duplicated, or shared mutable state).
  • Prefer safe designs to static mut. An AtomicUsize, Mutex<T>, RwLock<T> or OnceLock<T> gives global state without unsafe.
  • Lazy initialization: OnceLock (stable since 1.70) and LazyLock (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.
  • 'static bound does not mean "global". T: 'static only says that T holds no borrowed data shorter than the whole program. An owned String satisfies it. Thread spawning requires it for this reason.
  • Leaking with Box::leak produces a &'static mut T at run time when you truly need one.
  • Thread-local alternatives: thread_local! gives each thread its own copy and avoids Sync requirements.