std-method

Box::pin: own a pinned heap value

Allocate and pin an owned value (zero-sized values may need no allocation). Moving the returned owning handle need not relocate the value. This is a safe way to pin non-Unpin futures. Expected stdout (not executed): true Common mistake: Confusing movement of the Box handle with movement of its target.

Formal Syntax & Structure

rust syntaxrust
Box::pin(value: T) -> Pin<Box<T>>

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 });    let addr = std::ptr::from_ref(p.as_ref().get_ref());    let moved = p;    println!("{}", std::ptr::eq(addr, std::ptr::from_ref(moved.as_ref().get_ref())));}
Rust Reference