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

sync vs async manually

fn sync_func() -> u32 {
    println!("sync_func called");
    42
}

async fn async_func() -> u32 {
    println!("async_func called");
    42
}

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

    let value = sync_func();
    println!("Value: {}", value);

    let future = async_func();

    println!("Before block");
    let res =rt.block_on(future);
    println!("Result: {}", res);

    println!("End");
}
Start
sync_func called
Value: 42
Before block
async_func called
Result: 42
End
[package]
name = "demo"
version = "0.1.0"
edition = "2024"

[dependencies]
tokio = { version = "1.47.1", features = ["full"] }