lifetime

Rust Compare static reference and static type bounds

A static reference can remain valid for the duration of the program. A static type bound instead rules out types carrying shorter borrowed lifetimes; it does not require each value of that type to live forever. Owned String values can satisfy the bound and still be dropped normally. Expected output (not executed): Open Temporary owner finished Why: The literal reference is static, and the owned String carries no shorter borrowed lifetime. The String is consumed and dropped by the call. Common mistake: Trying to pass a reference to a locally owned String to consume, or leaking a value merely to satisfy T: 'static.

Formal Syntax & Structure

rust syntaxrust
&'static str  |  T: 'static

Working Example

Example Snippetrust
fn consume<T: std::fmt::Display + 'static>(value: T) {    println!("{value}");}fn main() {    let banner: &'static str = "Open";    consume(banner);    consume(String::from("Temporary owner"));    println!("finished");}
Rust Reference