Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Future and Async

Overview

The Future trait is the foundation of Rust’s async/await system, representing values that may not be ready yet.

Code Example

#![allow(unused)]
fn main() {
use std::future::Future;
use std::task::{Context, Poll, Waker};
use std::pin::Pin;

struct SimpleFuture {
    value: i32,
}

impl Future for SimpleFuture {
    type Output = i32;
    
    fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
        Poll::Ready(self.value)
    }
}

let future = SimpleFuture { value: 42 };
let mut boxed_future = Box::pin(future);

// Create a dummy waker for polling
struct NoOpWaker;
impl std::task::Wake for NoOpWaker {
    fn wake(self: std::sync::Arc<Self>) {}
}
let waker: Waker = std::sync::Arc::new(NoOpWaker).into();
let mut cx = Context::from_waker(&waker);

match boxed_future.as_mut().poll(&mut cx) {
    Poll::Ready(value) => println!("Future resolved with value: {}", value),
    Poll::Pending => println!("Future is pending"),
}
}

Key Concepts

  • Future: Async computation that may not be complete
  • Poll: Check if future is ready
  • Async/await: Built on top of Future trait

Learn More