#[tokio::main] turns async fn main into a sync main that starts a Tokio runtime
Answer: Annotate async fn main with #[tokio::main]. Asynchronous functions must be executed by a runtime, and the runtime does not automatically start; the macro transforms the async main into a synchronous fn main() that initializes a runtime instance and executes the async body.
What the macro expands to
#[tokio::main]
async fn main() {
println!("hello");
}
// becomes roughly:
fn main() {
let mut rt = tokio::runtime::Runtime::new().unwrap();
rt.block_on(async {
println!("hello");
})
}
Dependency
[dependencies]
tokio = { version = "1", features = ["full"] }
The tutorial enables the full feature flag; Tokio has a lot of functionality behind feature flags.
Async functions are lazy
Calling an async fn does not execute its body. An async fn returns an anonymous type that implements the Future trait, and the operation runs only when you use the .await operator on it:
async fn say_world() { println!("world"); }
#[tokio::main]
async fn main() {
let op = say_world(); // nothing printed yet
println!("hello");
op.await; // prints "world"
}
That program outputs hello then world.
What the runtime provides
The runtime contains the asynchronous task scheduler, provides evented I/O, timers, etc. An async fn is used for main because we want to enter an asynchronous context.
See also
Claims
- id: C1 claim: The macro transforms async main into a synchronous main. source: S1 evidence: "It transforms the async fn main() into a synchronous fn main()"
- id: C2 claim: The runtime does not start automatically. source: S1 evidence: "The runtime does not automatically start"
- id: C3 claim: The runtime contains the task scheduler. source: S2 evidence: "The runtime contains the asynchronous task scheduler"
Sources
- [S1] Hello Tokio. Tokio tutorial. https://tokio.rs/tokio/tutorial/hello-tokio . License: MIT. Accessed 2026-11-18.
- [S2] hello-tokio.md (source page). tokio-rs/website (GitHub). https://github.com/tokio-rs/website/blob/master/content/tokio/tutorial/hello-tokio.md . License: MIT. Accessed 2026-11-18.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Tokio tutorial, Hello Tokio; tokio-rs/website (GitHub), hello-tokio.md (source page). MIT (https://raw.githubusercontent.com/tokio-rs/website/master/LICENSE). Copyright Tokio contributors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Tokio tutorial, Hello Tokio; tokio-rs/website (GitHub), hello-tokio.md (source page). MIT (https://raw.githubusercontent.com/tokio-rs/website/master/LICENSE). Copyright Tokio contributors.
Cite this
iinventedlight. 2026. "The tokio::main attribute turns async fn main into a sync main that starts a Tokio runtime." iinventedlight Library, iil:note:2qk5p8kv, version of 2026-11-18. https://iinventedlight.com/library/notes/rust-tokio-main-macro/. License: CC-BY-4.0.
@misc{iil_note_2qk5p8kv,
author = {{iinventedlight}},
title = {{The tokio::main attribute turns async fn main into a sync main that starts a Tokio runtime}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-18},
url = {https://iinventedlight.com/library/notes/rust-tokio-main-macro/},
note = {Permanent ID iil:note:2qk5p8kv. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/rust-tokio-main-macro.md.