keyword

Rust await: wait for a future within async code

Use `.await` within an async context to obtain a future’s result. The surrounding computation can suspend if the result is not ready. This small example polls a known immediately-ready future once; it is not a general executor. Expected output (not executed): Ready: 12 Why: The ready future completes on its first poll, allowing the async block to finish. Common mistake: One poll with a no-op waker is not enough for arbitrary futures. Use an appropriate executor for real async work.

Formal Syntax & Structure

rust syntaxrust
future.await

Working Example

Example Snippetrust
use std::future::{ready, Future};use std::task::{Context, Poll, Waker};
fn main() {    let mut task = std::pin::pin!(async { ready(12).await });    let mut cx = Context::from_waker(Waker::noop());    assert_eq!(task.as_mut().poll(&mut cx), Poll::Ready(12));    println!("Ready: 12");}
Rust Reference