Poll and wakers
Quick answer
A Future exposes poll, which returns Ready(output) when finished or Pending when it cannot yet complete. A Waker tells the executor that a task should be polled again.
Model the state transition
poll(self: Pin<&mut Self>, cx: &mut Context<'_>) must return promptly. A pending future arranges notification through the current waker when it can make progress; waking does not imply immediate execution or readiness. Never repoll a completed future unless its own contract explicitly permits it. The manual samples use Waker::noop for controlled steps, not real scheduling.
use std::{future::{Future, ready}, task::{Context, Poll, Waker}};fn main() { let mut f = std::pin::pin!(ready(8)); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(8)); println!("ready");}Expected stdout (not executed):
readyNo external resource needs to become ready.
use std::{future::{Future, poll_fn}, task::{Context, Poll, Waker}};fn main() { let mut first = true; let mut f = std::pin::pin!(poll_fn(|cx| { if first { first = false; cx.waker().wake_by_ref(); Poll::Pending } else { Poll::Ready(9) } })); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Pending); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(9)); println!("pending -> ready");}Expected stdout (not executed):
pending -> readyA real executor would arrange the second poll using the wake notification. State diagram: created → first poll → Pending → wake schedules another poll → Ready. Pending is the suspension point.
Common mistake
Never implement a busy loop that continuously polls pending network futures.
Try it yourself
Draw the three states: not polled, pending, ready; label the wake edge as scheduling rather than completion.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Poll and wakers — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A future returns Pending because a resource is not ready. What mechanism should request another poll later?
Poll and wakers — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{future::{Future, ready}, task::{Context, Poll, Waker}}; fn main() { let mut f = std::pin::pin!(ready(8)); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(8)); println!("ready"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Poll and wakers — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust use std::{future::{Future, poll_fn}, task::{Context, Poll, Waker}}; fn main() { let mut first = true; let mut f = std::pin::pin!(poll_fn(|cx| { if first { first = false; cx.waker().wake_by_ref(); Poll::Pending } else { Poll::Ready(9) } })); let mut cx = Context::from_waker(Waker::noop()); assert_eq!(f.as_mut().poll(&mut cx), Poll::Pending); assert_eq!(f.as_mut().poll(&mut cx), Poll::Ready(9)); println!("pending -> ready"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.