Chunk a Vec of Futures

This works:

use futures::future::join_all;

#[tokio::main]
async fn main() {
    async fn foo(i: u32) -> u32 { i }
    let futures = vec![foo(1), foo(2), foo(3), foo(4), foo(5), foo(6)];
    join_all(futures).await;
}

But now I'd like to await the futures in chunks of 2, so I tried:

use futures::future::join_all;

#[tokio::main]
async fn main() {
    async fn foo(i: u32) -> u32 { i }
    let futures = vec![foo(1), foo(2), foo(3), foo(4), foo(5), foo(6)];
    for chunk in futures.chunks(2) {
        join_all(chunk).await;
    }
}

But because (I think) .chunks() returns a slice, this results in:

error[E0277]: `&impl futures::Future<Output = u32>` is not a future

Maybe chunks_mut works?

1 Like

chunks_mut() should indeed work. Future is not implemented for &Fut, but it is for &mut Fut.