lifetime

Rust Covariance of shared references

Covariance preserves the direction of a subtype relationship. Shared references permit this for both the borrow duration and the referenced type. A read-only view of a permanent label can therefore fit an interface that also reads a short-lived label. Expected output (not executed): Order: Packed Why: The literal can be viewed using the common shorter lifetime chosen for the call. Common mistake: Assuming equal lifetime annotations force equal storage duration.

Formal Syntax & Structure

rust syntaxrust
&'a T

Working Example

Example Snippetrust
fn show_pair<'a>(left: &'a str, right: &'a str) {    println!("{left}: {right}");}fn main() {    let status = String::from("Packed");    show_pair("Order", &status);}
Rust Reference