expression

Pinned data and interior mutability

Pinning is not immutability. A field that is intentionally independent of the address-sensitive state can use safe interior mutability. This example has no self-reference and does not expose a mutable reference to the containing record. Expected stdout (not executed): 3 Common mistake: Treating pinning as a universal freeze of every field.

Formal Syntax & Structure

rust syntaxrust
Pin<&T> with Cell<T> field

Working Example

Example Snippetrust
fn main() {    use std::{cell::Cell, marker::PhantomPinned};    struct Counter { n: Cell<u32>, _pin: PhantomPinned }    let p = Box::pin(Counter { n: Cell::new(2), _pin: PhantomPinned });    p.n.set(p.n.get() + 1);    println!("{}", p.n.get());}
Rust Reference