expression

Cooperative yielding

Offer the scheduler a chance to resume other work during a bounded task. Yielding does not promise another task will run next or establish a synchronization relationship. Tokio is an external dependency; see the proposed Cargo manifest. Expected stdout (not executed): 10 Common mistake: Using yield_now to guarantee a particular task ordering.

Formal Syntax & Structure

rust syntaxrust
tokio::task::yield_now().await

Working Example

Example Snippetrust
#[tokio::main(flavor = "current_thread")]async fn main() {    let task = tokio::spawn(async {        let mut total = 0;        for i in 1..=4 {            total += i;            tokio::task::yield_now().await;        }        total    });    println!("{}", task.await.unwrap());}
Rust Reference