Tokio runtime and task spawning
Quick answer
Tokio supplies an executor, task scheduling and optional I/O and timer drivers. Its current-thread runtime runs scheduled tasks on one thread; its multi-thread runtime can distribute tasks across worker threads.
Configure the runtime and own task inputs
For this chapter use a scratch Cargo project, edition 2024, with tokio = { version = "=1.44.2", features = ["rt", "rt-multi-thread", "macros", "sync", "time", "io-util", "test-util"] }. Put each program in src/main.rs, each test in src/lib.rs. #[tokio::main] builds and drives a runtime; spawn requires Send + 'static for the future and its output. spawn_blocking is for blocking work; bound its usage for CPU-heavy jobs. Join handles report task results or join errors.
#[tokio::main(flavor = "current_thread")]async fn main() { let input = String::from("queue"); let job = tokio::spawn(async move { input.len() }); println!("{}", job.await.unwrap());}Manual local Rust/Cargo candidate; not executed. Expected behavior:
5The future owns its input, rather than borrowing a parent stack frame.
#[tokio::main(flavor = "current_thread")]async fn main() { let result = tokio::task::spawn_blocking(|| (1..=4).sum::<u32>()).await.unwrap(); println!("{result}");}Manual local Rust/Cargo candidate; not executed. Expected behavior:
10This tiny example demonstrates the interface, not a performance improvement.
Common mistake
Do not assume aborting a running spawn_blocking closure stops it.
Try it yourself
Use a multi-thread runtime with two worker threads; retain the same expected result.
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
Tokio runtime and task spawning — practice quiz
Choose one answer per question. Code examples are predictions; review explanations after attempting them.
A Tokio task returns a value that the parent needs. How should the parent obtain it?
Tokio runtime and task spawning — 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 input = String::from("queue"); let job = tokio::spawn(async move { input.len() }); println!("{}", job.await.unwrap()); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.
Tokio runtime and task spawning — 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::task::spawn_blocking(|| (1..=4).sum::<u32>()).await.unwrap(); println!("{result}"); } ``` What standard output should the program produce if its run succeeds? Ignore Cargo and diagnostic messages.