The 'static Lifetime
The 'static lifetime is a special built-in lifetime in Rust that denotes references valid for the entire remaining duration of the running program, as well as type bounds (T: 'static) requiring that a type contain no temporary non-'static references.
How &'static T References Are Created
There are four primary ways to obtain a reference with the 'static lifetime:
- String and byte literals (
"hello",b"bytes"), which are embedded directly in the compiled binary's read-only data segment (.rodata). - References to
staticitems (static CONFIG: ...). - Compile-time constant promotion (
&42,&[1, 2, 3]), where the compiler promotes an immutable constant rvalue to static storage. - Explicitly leaked heap allocations via
Box::leakorString::leak, which intentionally drop ownership so the allocation lives until the process exits.
Compiled Binary (.rodata segment)
+-----------------------------------+
| "Rust 2024" (bytes embedded) | <--- `s: &'static str` points here
+-----------------------------------+ Valid for the entire program run
static MAX_CONNECTIONS: u32 = 1_024;
fn main() {
// 1. String literal:
let greeting: &'static str = "Hello, world!";
// 2. Reference to a `static` item:
let max_ref: &'static u32 = &MAX_CONNECTIONS;
// 3. Promoted constant slice literal:
let primes: &'static [u32] = &[2, 3, 5, 7, 11];
// 4. Runtime heap value intentionally leaked for the rest of the program:
let dynamic_str = format!("PID-{}", 9001);
let leaked_ref: &'static str = Box::leak(dynamic_str.into_boxed_str());
println!("{greeting} | max={max_ref} | primes={primes:?} | {leaked_ref}");
}
Hello, world! | max=1024 | primes=[2, 3, 5, 7, 11] | PID-9001
Coercing &'static T to Any Shorter Lifetime 'a
Because 'static outlives every other lifetime 'a ('static: 'a), a &'static T reference can be passed anywhere a &'a T reference is expected:
fn first_word<'a>(text: &'a str) -> &'a str {
text.split_whitespace().next().unwrap_or("")
}
fn main() {
let static_literal: &'static str = "static lifetime example";
let local_string = String::from("local lifetime example");
// Both `&'static str` and a short-lived `&str` work with `first_word`:
println!("From static: {}", first_word(static_literal));
println!("From local: {}", first_word(&local_string));
}
From static: static
From local: local
Two Meanings of 'static: Reference vs. Trait Bound
| Syntax | Meaning | Examples that Match |
|---|---|---|
&'static T |
Reference pointing to data that lives in memory for the rest of the program | "literal", &STATIC_VAR, Box::leak(...) |
T: 'static |
Type T owns its data or only holds 'static references (no temporary &'a borrows) |
i32, String, Vec<u8>, &'static str |
Why Compiler Suggestions to Add 'static Should Be Treated Carefully
When a function attempts to return a dangling reference or a borrow does not live long enough, rustc sometimes suggests &'static in diagnostic help messages. Adding 'static rarely fixes a genuine local borrow error—instead, either return an owned value (String) or tie the return lifetime to an input parameter ('a).