
Understand it
join runs futures at the same time and waits for all of them; select takes whichever finishes first. Dropping an owned operation future stops polling that operation, but completed external effects remain. A handle to independently spawned work is different: dropping a Tokio JoinHandle detaches the task rather than stopping it.
join! polls several futures concurrently and waits for all of them; it does not imply they run on separate threads. select! waits for one branch to become ready and then proceeds with that branch. The other in-progress branch futures may be dropped when the selection expression ends, depending on how they are owned. Dropping the actual operation future prevents its later polls; cleanup of owned values runs, but ordinary code after its suspension point does not. External effects already performed are not automatically rolled back.
In the example, loading a user and loading orders are independent, so join! can overlap their waits. If one result is enough, select! might reduce latency, but the losing operation must be safe to stop or resume. A database write followed by an acknowledgement is a dangerous candidate if cancellation can occur between them. Place durable state changes and retries behind an idempotent operation or an explicit transaction where appropriate.
Try timing two independent sleeps with sequential awaits and then with join!. Predict the total duration. Next, use select! between a timer and a slow operation, and record which cleanup code runs when the slow future loses. Do not assume async cancellation works like killing an OS thread; it occurs when an owned future is dropped, usually at a suspension boundary. Choose the combinator from the result you need and the cancellation guarantees of each branch.
When using a timeout wrapper, inspect whether it owns the inner future or only borrows it. That determines whether the timed-out operation is dropped or can continue elsewhere. In either case, record which effects are safe to repeat. If independent operations can fail separately, also decide whether join! should wait for every outcome or stop early through a different combinator.
Task ownership matters in timeouts: timeout(limit, operation()) owns the operation, while timing out a spawned task’s owned JoinHandle can merely drop the handle and leave the task running. To request cancellation of an async Tokio task, retain the handle, call abort(), and await it to observe termination. Cancellation is cooperative and cannot interrupt a poll that never yields; an already running spawn_blocking closure cannot be stopped this way. Use a shutdown protocol for work with effects that must finish or roll back explicitly.
Read the code
use tokio::join;
async fn load_user() -> &'static str { "ada" }
async fn load_orders() -> usize { 3 }
async fn run() {
let (user, orders) =
join!(load_user(), load_orders());
println!("{user} has {orders} orders");
}join overlaps the waits instead of adding them up.
Practice starter
let a = load_user().await;
let b = load_orders().await;Try it
Run two independent awaits concurrently rather than in sequence.
What success looks like: Total time is the slower of the two, not their sum.
Check your understanding
What is wrong with that if the two are independent?
Reveal the answer
- Nothing — two awaits already overlap by default
- They run one after the other, so the waits add up
- await cannot appear twice in one function
Sequential awaits serialise work that could have overlapped.
Go deeper
Common misconception: “Two .await lines one after the other already overlap.”
Each .await finishes before the next line starts, so the waits add up. join! polls both futures together, so the total is the slower of the two.
- Build both futures:
load_user()andload_orders()only build futures here;join!receives both before either body runs. - Poll together:
join!polls both inside this one task, so while one is waiting the other can still advance, with no extra thread. - Collect the tuple:
join!completes only when both finish, then returns(user, orders), so the line readsada has 3 orders.
Transfer challenge: Give each function a sleep of a different length and time it with sequential .await and with join!. Then race them with select! and write down what runs for the future that loses.
Connections
- Runtimes: Why async needs an executor
- Scoped Threads: Borrow local data while threads are guaranteed to join
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Async Patterns (courseId "concurrency", lessonId "async-patterns") Link: https://crabrix.com/learn/concurrency/async-patterns/ Goal: Dropping an owned operation future stops polling it; dropping Tokio’s JoinHandle detaches its spawned task. Practice: give me the lesson's exercise (crabrix_academy_get_exercise); I will write and run the code in Crabrix. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.