static

Question: Whats the difference between ‘static reference and ‘static trait bound?

The two uses of ‘static in Rust look similar but mean very different things:

  • “&‘static T` — A Static Reference:

A &‘static T is a reference that lives for the entire duration of the program. The data it points to must be stored somewhere that never gets freed — typically in the binary itself (e.g., string literals) or in a static variable. rust-lang

let s: &'static str = "hello world"; // baked into the binary

The key constraint here is on the reference itself: the pointed-to data must outlive everything. rust-lang

  • T: 'static — A Static Trait Bound:

A T: ‘static bound does not mean T is a reference at all. It means the type T contains no non-static references — i.e., T holds no borrowed data that could expire.

fn generic<T: 'static>(x: T) { ... }

Crucially, any fully owned type (like String, Vec, u32) automatically satisfies T: ‘static, because it has no internal borrowed references that could go stale. A &‘static str also satisfies it, but a &‘a str (with a non-static lifetime) does not. rust-lang

It is a common misconception that ‘static always means “lives forever”, but this is not the case. Just because a value is ‘static does not mean that you have a memory leak. You can read more in Common Rust Lifetime Misconceptions.

Question: When T: 'static Is Needed?

The T: ‘static bound is commonly required when values must escape their creation scope, such as when spawning threads with thread::spawn. Since a spawned thread can outlive the caller, Rust requires that anything sent into it contains no short-lived references — which is exactly what T: ‘static guarantees. Rust Traits are not interfaces, Learning Rust: static trait bounds

// thread::spawn requires T: Send + 'static
fn run_in_background<T: Send + 'static>(val: T) {
    std::thread::spawn(move || { /* use val */ });
}

A useful mental shortcut: &‘static T is about where data lives; T: ‘static is about whether a type is safe to keep indefinitely.