trait

AsyncFnMut: mutable async callback bound

An async callback can carry state across calls, with mutable access borrowed by each returned future. Sequential awaiting allows those borrows to finish before the next call. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 7 Common mistake: Treating AsyncFnMut as a guarantee that multiple calls may overlap.

Formal Syntax & Structure

rust syntaxrust
F: AsyncFnMut(u32) -> u32

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"),        }    }    use std::ops::AsyncFnMut;    async fn run<F: AsyncFnMut(u32) -> u32>(mut f: F) -> u32 {        let _ = f(2).await;        f(5).await    }    let mut count = 0;    let f = async |n: u32| { count += n; count };    println!("{}", ready_now(run(f)));}
Rust Reference