skip to content

In Rust, what does the bound `T: 'static` require of a type, and why does `String` satisfy it while `&'a str` does not?

level: middleimportance: must knowfreq 62%

answer

  1. a bound on a type, not a value
  2. no borrow that could expire
  3. owned data passes trivially
  4. dropping early is still allowed

basics

~20 s

T: 'static means the type holds no borrow shorter than 'static: it owns its data or borrows only 'static data. String owns its bytes; &'a str with a shorter 'a does not qualify. The value can still be dropped anytime.

solid answer

~40 s

The Reference defines `T: 'a` as "all lifetime parameters of `T` outlive `'a`". So `T: 'static` says the type contains **no borrow that could expire**: it either owns everything (`String`, `Vec<u64>`, `Box<str>`, `i32`) or borrows only `'static` data (`&'static str`). `String` has no lifetime parameter, so it passes trivially, while `&'a str` fails whenever `'a` is shorter than the program. The bound does **not** mean the value lives forever: a `String` passed to a `T: 'static` function can be dropped a line later. It means the holder may keep the value as long as it likes without a dangling borrow. In generic code, forgetting it gives E0310, `the parameter type T may not live long enough`.

code

rust · 14 lines
rust
use std::any::Any;

fn keep<T: 'static>(value: T) -> Box<dyn Any> {
    Box::new(value)
}

fn main() {
    let owned = String::from("cpu");
    let a = keep(owned);              // String: 'static, though it is a heap value
    let b = keep("mem");              // &'static str: 'static
    let c = keep(vec![1_u64, 2, 3]);  // Vec<u64>: 'static
    drop(a);                          // a 'static value can still be dropped early
    println!("{} {}", b.is::<&str>(), c.is::<Vec<u64>>());
}

go deeper

for a junior

Recall that owned types like String and Vec<u64> satisfy T: 'static, and that a borrow of a local does not.

for a middle

Explain the Reference's rule that every lifetime parameter inside T must outlive 'static, contrast &'static T with T: 'static, and show a T: 'static value being dropped early.

for a senior

Diagnose E0310 in generic code and E0597 at call sites, decide whether the bound belongs in the helper, and steer callers toward owned values rather than leaks.

for a principal

Weigh where 'static bounds belong in a public API: they keep internals simple for spawning and type erasure but force callers holding borrows to allocate or share ownership.

## Two different uses of `'static` Rust writes `'static` in two places that read alike and mean different things: - In a **reference type**, `&'static T` is a borrow that stays valid until the program ends. It is a property of one pointer. - In a **bound**, `T: 'static` is a constraint on a type parameter. It says nothing about any pointer being valid forever; it limits what the type is allowed to contain. Interviewers ask about the difference because the first reading leads candidates to believe `T: 'static` values are immortal, which is false. One way to keep them apart: `&'static str` satisfies both readings, a `String` satisfies only the bound because it is not a reference at all, and a `&'a str` borrowed from a local satisfies neither. ## What the bound actually requires The Reference's rule for lifetime bounds on types is: `T: 'a` means that all lifetime parameters of `T` outlive `'a`. It gives the examples directly: `i32: 'static` holds, `&'static str: 'a` holds for any `'a`, and `Vec<&'a ()>: 'static` does not when `'a` is an unconstrained parameter. Applied to `'static`, a type passes when every borrow inside it is `'static`: | Type | `T: 'static`? | Why | |---|---|---| | `String`, `Vec<u64>`, `Box<str>`, `i32` | yes | no lifetime parameters at all; the data is owned | | `&'static str` | yes | its only borrow is `'static` | | `Sample<'static>` (a struct with a `&'a str` field) | yes | instantiated with `'static` | | `&'a str` for a shorter `'a` | no | the borrow could expire | | `Vec<&'a str>` for a shorter `'a` | no | it contains a borrow that could expire | So "owned or `'static` data" is the working summary: the value carries no ticking clock from someone else's stack frame. ## The bound does not keep anything alive A function `fn keep<T: 'static>(value: T)` accepts a freshly built `String` and its caller can drop the result immediately. The bound only guarantees that **if** the receiver chooses to hold the value indefinitely, nothing inside it will dangle. Seen from the receiver's side, the bound is a permission: a value of such a type can be stored in a struct with no lifetime parameter, moved to another thread or kept in a cache, and the compiler needs no further proof. That is why std puts it where a value may be held for an unknown time: 1. `std::thread::spawn` needs its closure to be `'static`, because the new thread may outlive the caller. 2. Type erasure through `std::any::Any`: the trait is declared `pub trait Any: 'static`, so only `'static` types can be boxed as `Box<dyn Any>` and downcast later. 3. Long-lived registries and caches that store values of a generic type with no lifetime parameter to tie them to. ## The diagnostics you will see Passing a borrow of a local into a `T: 'static` parameter gives E0597 with a note pointing at the bound: ```plaintext error[E0597]: `line` does not live long enough 10 | let boxed = keep(key); | --------- argument requires that `line` is borrowed for `'static` note: requirement that the value outlives `'static` introduced here 3 | fn keep<T: 'static>(value: T) -> Box<dyn Any> { ``` Writing a generic function that boxes `T` into `Box<dyn Any>` without the bound gives E0310, `the parameter type T may not live long enough`, and rustc's help suggests adding `T: 'static`. In a generic helper, adding the bound is correct: it states a requirement your body really has. ## Fixing a caller that holds a borrow When a caller has a `&str` sliced out of a local `String` and the API needs `T: 'static`, the options are: - pass an owned value instead, for example `key.to_string()`, which is a `String` and passes; - share ownership with a reference-counted pointer whose contents are owned, covered with smart pointers; - restructure so the borrow's owner outlives the use, which changes the API rather than the bound. ## Misreadings to avoid - **"`T: 'static` values are never dropped."** They are dropped like any value when their owner lets go. - **"`T: 'static` means `T` must be a reference."** Most types that satisfy it are not references at all. - **"`String` is `'static` because it lives on the heap."** Heap placement is irrelevant; `Vec<&'a str>` is also on the heap and fails. What matters is the absence of short borrows.

  • Does passing a value to a `T: 'static` parameter keep it alive until the program ends?
    No. The bound restricts the type, not the value's lifetime. A `String` passed in is dropped whenever its owner drops it, possibly a line later. The bound only promises that nothing inside the value can dangle if the receiver chooses to hold it indefinitely.
  • Why does a generic `fn erase<T>(v: T) -> Box<dyn Any>` fail without a bound?
    `Any` is declared `pub trait Any: 'static`, so a `T` holding a short borrow cannot become `Box<dyn Any>`. rustc 1.99 reports E0310, `the parameter type T may not live long enough`, and suggests `T: 'static`, which is the correct fix because the body genuinely needs it.
  • Is `Vec<&'static str>` `'static`? What about `Vec<&'a str>`?
    `Vec<&'static str>` is: its only borrow is `'static`. `Vec<&'a str>` is not when `'a` is shorter, because the Reference's rule requires every lifetime parameter inside the type to outlive `'static`. Being heap-allocated does not change that.

A long-term storage unit that accepts only things you own outright or things on permanent loan. The rule says nothing about how long your box stays inside; you can collect it tomorrow. It only guarantees that nobody can recall an item while it sits there.

  • A value of a T: 'static type is kept alive until the program exits.
  • T: 'static means T must be a reference such as &'static str.
  • String satisfies 'static because its bytes live on the heap.
  • Any Vec is 'static, because a Vec owns its elements.
  • Adding T: 'static to a generic helper forces callers to leak memory.