lifetime

Rust Interior mutability and invariant slots

Some types permit changing stored data through shared access. Cell is an example, and its stored type is invariant. A shared reference to a Cell does not make the Cell contents covariant. This preserves the slot’s type guarantee. Expected output (not executed): Sent Why: Cell allows a shared handle to update the value without changing the declared inner type. Common mistake: Assuming that a shared outer reference makes every nested type covariant.

Formal Syntax & Structure

rust syntaxrust
std::cell::Cell<T>

Working Example

Example Snippetrust
use std::cell::Cell;fn main() {    let label: Cell<&'static str> = Cell::new("Queued");    let view = &label;    view.set("Sent");    println!("{}", label.get());}
Rust Reference