macro

pin!: local pinning without a new heap allocation

The macro gives a locally borrowed pin without creating a heap allocation of its own. In async code the local may reside in future storage, which can be on the heap. The borrowed pin cannot outlive its storage. Expected stdout (not executed): 7 Common mistake: Returning a pin to a temporary created inside a function.

Formal Syntax & Structure

rust syntaxrust
std::pin::pin!(value) -> Pin<&mut T>

Working Example

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