std-type

Rust Cell<T>

Cell is suitable for replacing a value or copying a small Copy payload without exposing an ordinary shared reference to that payload. It can hold non-Copy values too, but get is restricted to Copy. Cell is not Sync. Expected output (not executed): 12 Why: Both bindings access the same cell; set replaces its contents. Common mistake: Assuming Cell<T> requires T: Copy for all operations.

Formal Syntax & Structure

rust syntaxrust
Cell::new(value)

Working Example

Example Snippetrust
use std::cell::Cell;fn main() {    let score = Cell::new(8);    let view = &score;    view.set(12);    println!("{}", score.get());}
Rust Reference