Sub topic 10
'static: string literals and T: 'static
This chapter assumes you can read lifetime annotations like &'a str, and that you know what a generic bound such as T: Debug means.
The 'static lifetime
'static is the longest lifetime. A reference with this lifetime is valid for the whole run of the program. The most common source is a string literal. The text of a literal is stored in the compiled program itself, so it is never freed while the program runs. That is why its type is &'static str.
fn main() {
let s: &'static str = "literal";
println!("{s}");
}The annotation is optional here. Writing let s = "literal"; gives the same type.
'static as a reference lifetime
When 'static appears on a reference, it describes data that is never dropped before the program ends. Literals are the usual case. Constants and static items also qualify.
A function can return &'static str without taking any input reference:
fn greeting() -> &'static str {
"hello"
}
fn main() {
println!("{}", greeting());
}Do not add 'static to make a lifetime error go away. If the data lives in a local String, the annotation only moves the error somewhere else, because the compiler checks that the data really lives long enough.
T: 'static as a bound
T: 'static is a different idea. It is a bound on a type parameter. It says that T contains no references that could expire before the program ends. Two kinds of type satisfy it:
- Owned types such as
String,Vec<i32>, andi32, which hold no borrowed data. - Types whose references are all
'static, such as&'static str.
use std::fmt::Debug;
fn print_it<T: Debug + 'static>(t: T) {
println!("{t:?}");
}
fn main() {
let s: &'static str = "literal";
print_it(s);
print_it(String::from("owned"));
// let local = String::from("borrowed");
// print_it(&local); // error: `local` does not live long enough
}The first two calls compile. The commented-out call would fail, because &local is a reference to a local variable and cannot be 'static.
Where you will meet T: 'static
The standard library's std::thread::spawn requires its closure to be 'static. A new thread may outlive the function that started it, so it cannot borrow from that function's locals. The usual fix is to move owned data into the closure:
use std::thread;
fn main() {
let name = String::from("worker");
let handle = thread::spawn(move || {
println!("hello from {name}");
});
handle.join().unwrap();
}Without move, the closure would borrow name, and the compiler would reject it with an error about the closure possibly outliving the borrowed value. Moving ownership in satisfies the bound.
Quick comparison
| Form | Meaning | Example |
|---|---|---|
| `&'static T` | A reference valid for the whole program | `"literal"` |
| `T: 'static` | `T` holds no non-`'static` borrows | `String`, `&'static str` |
Exercise
Does print_it(vec![1, 2, 3]) compile when print_it requires T: Debug + 'static? Does print_it(&vec![1, 2, 3]) compile when the vector is a local variable?
Show answer
The first call compiles. A Vec<i32> is an owned type with no references, so it satisfies 'static. The second call fails. It passes a reference to a local variable, and that reference cannot live for the whole program, so the bound is not met.