What is Pin and why is it needed for async

Pin is a smart pointer that prevents memory movement to ensure async functions can safely resume execution.

Pin is a smart pointer that prevents a value from being moved, which is required for async functions to safely resume execution on the same memory address. Without Pin, the compiler cannot guarantee that a future's internal state remains valid after it is suspended and resumed by the executor.

use std::pin::Pin;

// Pin ensures the value at the pointer cannot be moved
let pinned: Pin<Box<dyn Future>> = Box::pin(async { /* code */ });