lifetime

Rust Invariance of the referenced type

Invariance prevents deriving a conversion merely from a subtype relationship between inner types. A writable slot must preserve its declared type. Otherwise code could put a short-lived reference into a slot that promises a permanent reference. Expected output (not executed): Closed Why: The replacement satisfies the exact inner reference type required by the slot. Common mistake: Trying to write a reference to a temporary String into this slot.

Formal Syntax & Structure

rust syntaxrust
&mut T

Working Example

Example Snippetrust
fn replace_label(slot: &mut &'static str) {    *slot = "Closed";}fn main() {    let mut label: &'static str = "Open";    replace_label(&mut label);    println!("{label}");}
Rust Reference