Start a Tokio runtime with the
Turn async fn main into a program with the tokio::main attribute, which builds a multi-thread Tokio 1.40 runtime and runs your body on it with block_on.
async fn main does not compile on its own, because nothing in the standard library can run a future. The #[tokio::main] attribute fixes that by rewriting main into an ordinary function that builds a runtime and calls block_on on your body. This is the usual entry point for a Tokio 1.40 binary on Rust 1.85.
use std::time::Duration;
use tokio::time::sleep;
#[tokio::main]
async fn main() {
println!("start");
sleep(Duration::from_millis(10)).await;
println!("done");
}start
doneYou need the macros and rt-multi-thread features, or full. The macro defaults to the multi-thread scheduler with one worker per CPU core. Two arguments change that: #[tokio::main(flavor = "current_thread")] runs everything on the calling thread, and #[tokio::main(worker_threads = 2)] fixes the pool size. The detail worth knowing is that the macro is plain sugar. The expansion is tokio::runtime::Builder::new_multi_thread().enable_all().build().unwrap().block_on(async { ... }), so when you need a custom thread name or a runtime inside a larger sync program, drop the attribute and build it yourself. See the tokio::main docs.