expression

Unchecked pinning: obligations before unsafe construction

Unchecked construction is a promise about the entire lifetime of the pinned target and behavior of the pointer, not just the lifetime of a temporary Pin handle. Breaking the promise can invalidate safe abstractions. Prefer safe local or heap construction; the executable illustration uses only Box::pin. Expected stdout (not executed): 7 Common mistake: Using new_unchecked simply to silence the Unpin bound or assuming dropping the wrapper ends the promise.

Formal Syntax & Structure

rust syntaxrust
unsafe { Pin::new_unchecked(pointer) } (syntax only; not executed)

Working Example

Example Snippetrust
fn main() {    use std::{marker::PhantomPinned, pin::Pin};    struct Note { value: u32, _pin: PhantomPinned }    let p = Box::pin(Note { value: 7, _pin: PhantomPinned });    println!("{}", p.as_ref().get_ref().value);}
Rust Reference