expression

Bridge synchronous code with block_on

Drive a future from synchronous code and return its result. Do not start a nested blocking runtime call from a Tokio async task. Reuse an existing runtime when calling many async operations from sync code. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 6 Common mistake: Calling block_on inside an already running async task.

Formal Syntax & Structure

rust syntaxrust
Runtime::block_on(future) -> F::Output

Working Example

Example Snippetrust
fn main() {    let rt = tokio::runtime::Builder::new_current_thread().build().unwrap();    let total: u32 = rt.block_on(async { [1u32, 2, 3].iter().sum() });    println!("{total}");}
Rust Reference