expression

Store different future types in one collection

Distinct async blocks have distinct anonymous types even when their outputs match. A common erased future type lets a collection hold different producers; this example runs them sequentially, not concurrently. The fixture-only ready_now helper polls once and rejects Pending; it is not an executor. Expected stdout (not executed): 2 12 Common mistake: Assuming Vec<impl Future> can hold unrelated anonymous future types or automatically schedule them.

Formal Syntax & Structure

rust syntaxrust
Vec<Pin<Box<dyn Future<Output=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::pin::Pin;    let jobs: Vec<Pin<Box<dyn Future<Output=u32>>>> = vec![Box::pin(async { 2 }), Box::pin(async { 3 * 4 })];    for job in jobs { println!("{}", ready_now(job)); }}
Rust Reference