liveBook · Manning

Poll

As explained in Learn Rust in a Month of Lunches

poll is the operation by which an asynchronous runtime advances a future toward completion and obtains its current state or output.

A Rust async function returns a Future: a value that may produce its output now or later. To poll a future is to check its progress. Each check returns Poll::Pending if the output is not ready, or Poll::Ready(value) if it is. section 19.3.1 section 19.3.2

pub enum Poll<T> {
    Ready(T),
    Pending,
}

Poll has two possible states. Only Ready carries the future’s output. section 19.3.1

Usually, you do not call the poll method yourself. Write .await inside an async function or block, and an async runtime manages the polling. If the future is not ready, the runtime can come back to it later. The poll method’s signature also mentions Pin and Context; understanding Pin in depth is not needed to get started. section 19.3.1 section 19.3.2 section 19.3.3

Awaiting a value

With #[tokio::main] above async fn main(), `let number = async_give_8().await;` gives number a u8 value. Without an async function or block, .await produces a compiler error; simply changing an ordinary main to async fn main() also fails without a runtime setup. section 19.3.2 section 19.3.3

How Learn Rust in a Month of Lunches covers it

How deep it goes
Defines Pending and Ready, shows the Future method signature, and defers a deep explanation of Pin.
Examples
Uses an async function returning u8, compiler errors for misplaced .await, and a working Tokio main function.
What it assumes
Introduces Future and polling from basics while assuming familiarity with ordinary Rust functions.
What you can do after
Apply it via .await and Tokio to obtain a future’s output without calling poll manually.
How it connects
Connects polling to Future, Poll, async functions, .await, and async runtimes.

Also explained in

Compare how 2 books explain poll →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.