expression
Offload finite blocking operations
Run a finite synchronous closure on the blocking pool rather than obstructing an async worker. The closure and result must be Send and static. Bound CPU-heavy work separately; the blocking pool is not an unlimited CPU executor. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 15 Common mistake: Calling a long blocking operation directly in an async task or spawning unbounded blocking jobs.
Formal Syntax & Structure
rust syntaxrust
tokio::task::spawn_blocking(|| ...).awaitWorking Example
Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() { let numbers = vec![3u32, 5, 7]; let task = tokio::task::spawn_blocking(move || numbers.into_iter().sum::<u32>()); println!("{}", task.await.unwrap());}