A JoinSet manages multiple async tasks and yields results as they complete.
A tokio::task::JoinSet is a collection that manages multiple spawned async tasks and allows you to wait for them to complete in the order they finish.
use tokio::task::JoinSet;
let mut set = JoinSet::new();
for i in 0..10 {
set.spawn(async move { i * 2 });
}
while let Some(result) = set.join_next().await {
println!("Task finished: {:?}", result);
}
Think of a JoinSet as a manager for a team of workers. You assign tasks to the team, and the manager waits until everyone is done, reporting back results as soon as each person finishes rather than waiting for the slowest worker. This is useful when you need to run many similar jobs at once and process their results as they arrive.