expression

Async closures: async |args|

An async closure produces a future when called. Its body runs when the future is polled. Rust 1.85 stabilizes this syntax; creating the closure and calling it do not by themselves run the body. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 5 Common mistake: Trying to add an integer directly to the returned future.

Formal Syntax & Structure

rust syntaxrust
let f = async |x: u32| -> u32 { x + 1 };

Working Example

Example Snippetrust
fn main() {    use std::{future::Future, task::{Context, Poll, Waker}};    fn ready_now<F: Future>(f: F) -> F::Output {        let mut f = std::pin::pin!(f);        let mut cx = Context::from_waker(Waker::noop());        match f.as_mut().poll(&mut cx) {            Poll::Ready(value) => value,            Poll::Pending => panic!("fixture expected immediate readiness"),        }    }    let plus = async |x: u32| x + 1;    let future = plus(4);    println!("{}", ready_now(future));}
Rust Reference