Await on variable number of futures

The select! macro allows to listen on multiple futures.

async fn race_tasks() {
    let t1 = task_one().fuse();
    let t2 = task_two().fuse();

    pin_mut!(t1, t2);

    select! {
        () = t1 => println!("task one completed first"),
        () = t2 => println!("task two completed first"),
    }
}

But it seems select! only allows to await on a fixed number of futures.

I have a variable number of streams, the number changes on the fly.
Is there a way to await on them?
When any of them is signalled, I want my code executed.

Check the documentation for FuturesUnordered: futures::stream::FuturesUnordered - Rust

That might be a better option for your use case.

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.