std-method

Box::into_pin: pin an existing Box

Turn an existing owning Box into a pinned Box without allocating another box or relocating its target. Pinning restrictions begin with the conversion, not the original allocation. Expected stdout (not executed): true Common mistake: Assuming Box::new by itself creates a pinning contract.

Formal Syntax & Structure

rust syntaxrust
Box::into_pin(boxed: Box<T>) -> Pin<Box<T>>

Working Example

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