Executors and tasks
Quick answer
An executor schedules and polls futures. A task is a unit of scheduled asynchronous work; multiple tasks can share a thread or run across multiple threads, depending on the runtime.
Keep polling cooperative
An await of a pending future yields control to the runtime; an await of a ready future may not yield. Blocking I/O or heavy computation inside a poll can starve other tasks. Tokio is an external runtime. For this chapter's Tokio candidates create a scratch project using edition 2024 and tokio = { version = "=1.44.2", features = ["rt", "rt-multi-thread", "macros", "sync", "time", "io-util", "test-util"] }. Use one example per src/main.rs unless marked as a test.
fn main() { let runtime = tokio::runtime::Builder::new_current_thread().build().unwrap(); let n = runtime.block_on(async { 3 * 4 }); println!("{n}");}Manual local Rust/Cargo candidate; not executed. Expected behavior:
12block_on drives the root future to completion.
#[tokio::main(flavor = "current_thread")]async fn main() { let a = tokio::spawn(async { 4 }); let b = tokio::spawn(async { 5 }); println!("{}", a.await.unwrap() + b.await.unwrap());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
9The tasks are concurrent even on a single-thread runtime.
Common mistake
Do not call blocking sleep inside an async task to simulate asynchronous waiting.
Try it yourself
Replace one task with a bounded computation sent to spawn_blocking and compare the ownership requirements.
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
Executors and tasks — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A CPU-heavy poll never yields on a single-thread executor. What can happen?
Executors and tasks — 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 fn main() { let runtime = tokio::runtime::Builder::new_current_thread().build().unwrap(); let n = runtime.block_on(async { 3 * 4 }); println!("{n}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Executors and tasks — 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 a = tokio::spawn(async { 4 }); let b = tokio::spawn(async { 5 }); println!("{}", a.await.unwrap() + b.await.unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.