Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Simple async example with Tokio

[package]
name = "simple"
version = "0.1.0"
edition = "2021"

[dependencies]
tokio = { version = "1.39.2", features = ["full"] }
async fn do_something() {
    println!("Start to do something");
    tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    println!("End   to do something");
}

fn main() {
    println!("Start");
    let rt = tokio::runtime::Runtime::new().unwrap();
    let future = do_something();

    println!("Before block");
    rt.block_on(future);

    println!("End");
}
Start
Before block
Start to do something
End   to do something
End