Crate Tokio

Chester Wyke August 21, 2023 Updated: April 15, 2025 #rust

Setup a runtime environment

Source: https://users.rust-lang.org/t/calling-async-function-from-main/53314

Use macro on main

#[tokio::main]
async fn main() {
    do_thing().await;
}
async fn do_thing() {
    println!("Yep I ran");
}

Manually create a runtime

Note: It’s important that you use block_on as if there isn’t something that is being awaiting the runtime stops. Can’t remember the source but I forgot to block on at least one thing and then I couldn’t spawn anything and have it run (wasted a lot of time).

fn main() {
    let rt = tokio::runtime::Runtime::new().unwrap();
    rt.block_on(do_thing());
}
async fn do_thing() {
    println!("Yep I ran");
}