trait

Future: the computation contract

A future exposes its completion type through Output and advances when polled. When the result is not available, a normal pending implementation arranges notification when progress is possible. Pin protects address-sensitive state; it does not itself execute code. Expected stdout (not executed): 15 Common mistake: Calling poll repeatedly without regard to notifications or after completion.

Formal Syntax & Structure

rust syntaxrust
trait Future { type Output; fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>; }

Working Example

Example Snippetrust
fn main() {    use std::{future::Future, task::{Context, Poll, Wake, Waker}, sync::Arc};    struct Noop;    impl Wake for Noop { fn wake(self: Arc<Self>) {} }    let w = Waker::from(Arc::new(Noop));    let mut cx = Context::from_waker(&w);    fn inspect<F: Future<Output = u32>>(future: F, cx: &mut Context<'_>) {        let mut f = std::pin::pin!(future);        if let Poll::Ready(n) = f.as_mut().poll(cx) { println!("{n}"); }    }    inspect(async { 15u32 }, &mut cx);}
Rust Reference