Join and select
Quick answer
tokio::join! waits for all its branch futures; tokio::select! returns after a matching branch completes and drops its owned losing branch futures.
Choose all results or one outcome
join polls branches concurrently within the same task, not necessarily in parallel. select is useful for competing events, such as a message or shutdown. When selecting borrowed futures, dropping a branch's borrow need not drop the underlying future. If multiple branches are ready, do not depend on default selection order. Consider cancellation safety before putting a multi-step I/O operation into select.
#[tokio::main(flavor = "current_thread")]async fn main() { let (a, b) = tokio::join!(async { 4 }, async { 6 }); println!("{}", a + b);}Manual local Rust/Cargo candidate; not executed. Expected behavior:
10Both branches finish and contribute to the sum.
#[tokio::main(flavor = "current_thread")]async fn main() { let result = tokio::select! { n = std::future::ready(7) => n, _ = std::future::pending::<()>() => 0, }; println!("{result}");}Manual local Rust/Cargo candidate; not executed. Expected behavior:
7Only one branch can complete in this controlled example.
Common mistake
Do not treat join as a parallel CPU executor or select as transactional rollback.
Try it yourself
Replace the pending branch with another ready value; describe both permitted outcomes.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Join and select — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
You race a shutdown signal against an owned operation. Which macro expresses this choice?
Join and select — example 1. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust #[tokio::main(flavor = "current_thread")] async fn main() { let (a, b) = tokio::join!(async { 4 }, async { 6 }); println!("{}", a + b); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Join and select — example 2. Read the code; do not execute it for this question. Assume the lesson’s stated setup, compatible dependencies and toolchain, and a fresh disposable directory for file-creating examples. ```rust #[tokio::main(flavor = "current_thread")] async fn main() { let result = tokio::select! { n = std::future::ready(7) => n, _ = std::future::pending::<()>() => 0, }; println!("{result}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.