lifetime

Rust Covariance of function outputs

Return types of function pointers preserve subtype direction. A callback returning a durable reference can satisfy an interface that promises only a shorter-lived reference. The shorter promise does not destroy or relocate the original data. Expected output (not executed): Warehouse Why: A pointer returning a static reference meets the shorter output contract. Common mistake: Assuming a function returning local borrowed data can satisfy an interface that promises a static reference.

Formal Syntax & Structure

rust syntaxrust
fn() -> T

Working Example

Example Snippetrust
fn label() -> &'static str { "Warehouse" }fn show<'a>(_: &'a str) {    let f: fn() -> &'a str = label as fn() -> &'static str;    println!("{}", f());}fn main() {    let session = String::from("visit");    show(&session);}
Rust Reference