expression

Sequential mutable async closure calls

A call may borrow the closure mutably until its future is completed or dropped. Await or otherwise finish the first call before making a second overlapping mutable call. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 3 7 Common mistake: Keeping two outstanding futures that both require mutable access to the same captured counter.

Formal Syntax & Structure

rust syntaxrust
let mut f = async |arg| { captured += arg; };

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 mut total = 0u32;    let mut add = async |n: u32| { total += n; total };    println!("{}", ready_now(add(3)));    println!("{}", ready_now(add(4)));}
Rust Reference