Async Rust · 19 min · free with an account

Async Patterns

Lesson preview · about 30%

Concurrency inside one task comes from combinators, not threads.

Core idea

Dropping a future cancels it at its last await point.

Start with the idea

join runs futures at the same time and waits for all of them; select takes whichever finishes first. Dropping a future cancels it at its last await point, which is a real advantage over threads — but it means anything that must not be interrupted needs to be written with that in mind.

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 a future is cancellation: code after its last completed await point does not run. 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!

Keep learning, for free

The rest of this lesson is waiting for you.

Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.

No payment details. Your account opens the complete Academy.

← All lessons in Concurrency & Async