Rust's standard library defines `Future` and `async`/`.await` but no runtime, so how does a Rust program actually run an async function from `main`?
answer
- std has the trait, not the engine
- main must stay synchronous
- E0752 on async fn main
- block_on bridges sync to async
basics
~20 sRust's std ships the Future trait and async syntax but no executor, and rustc rejects async fn main (E0752). A synchronous main passes the top-level future to an executor's block_on, such as futures::executor::block_on or smol::block_on.
solid answer
~40 sThe standard library defines `Future`, `Poll`, `Context`, `Waker` and the `async`/`.await` syntax, but it contains no executor and no I/O reactor, so a future only makes progress when some library code polls it. `main` itself cannot be `async`: rustc rejects it with E0752. The usual pattern is a synchronous `main` that calls a runtime's `block_on`, for example `futures::executor::block_on(run())` or `smol::block_on(run())`, which blocks the current thread polling the future until it returns its output. Runtime attribute macros such as Tokio's `#[tokio::main]` only rewrite `main` into building a runtime and calling its `block_on`. Inside async code you keep using `.await`; `block_on` belongs at the edge where synchronous code meets async code.
code
rust · 19 linesasync fn count_lines(text: &str) -> usize {
text.lines().count()
}
async fn run() -> usize {
let a = count_lines("one\ntwo").await;
let b = count_lines("three").await;
a + b
}
fn main() {
// `main` stays synchronous; block_on is the bridge into async code.
let total = futures::executor::block_on(run());
println!("{total} lines");
// The same future, driven by a different runtime's block_on.
let again = smol::block_on(run());
assert_eq!(total, again);
}go deeper
Recall that std has the Future trait but no executor, that async fn main is error E0752, and that a block_on call in a synchronous main is how async code starts.
Explain what block_on does on Pending (parking until the waker fires) and why the same future can be driven by futures' or smol's block_on when it uses no runtime-specific I/O.
Show where block_on belongs in a real codebase: only at synchronous edges such as main, tests and CLI handlers, never inside code an executor is already polling.
Frame the absence of a std runtime as a design choice your team must answer: which runtime the binaries standardise on and how libraries stay usable without forcing one.
## What std gives you, and what it leaves out Rust's async support is split between the **language and std**, which define the vocabulary, and **runtime crates**, which do the work. What you get from `core` and `std` is: - the `Future` trait, whose one method `poll` returns `Poll::Ready(value)` or `Poll::Pending`; - `Context` and `Waker`, the handle a pending future uses to ask to be polled again; - the `async fn`, `async` block and `.await` syntax, which rustc compiles into types implementing `Future`. What you do **not** get is an **executor** (the loop that decides which future to poll next) or a **reactor** (the part that waits on the operating system for sockets and timers and wakes the right future). There is no stable `block_on` in std and no way to spawn a task. A value of an `async fn`'s return type is inert until something polls it, so a program needs at least one piece of code, somewhere, that does the polling. ## Why `async fn main` is rejected The obvious first attempt is to mark `main` itself `async`. rustc refuses: ```plaintext error[E0752]: `main` function is not allowed to be `async` ``` The reason is the same gap: the program's entry point is called by the platform start-up code, which expects an ordinary function returning `()` or a `Termination` type. An `async fn main` would return a future, and nothing in std would poll it. The error-code index says the entry point is not allowed to be `async` and suggests declaring it without `async`; using `.await` inside a plain `fn main` fails separately with E0728, because `.await` is only allowed inside `async` functions and blocks. ## `block_on`: the bridge from synchronous code Every runtime offers a function that takes a future and **blocks the calling thread** until that future completes: | function | crate | what it does while the future is pending | |---|---|---| | `futures::executor::block_on` | futures 0.3 | parks the thread until the future's waker unparks it | | `smol::block_on` | smol 2 (re-export of `async_io::block_on`) | parks, and processes I/O and timer events on this thread while idle | | a runtime value's `block_on` | Tokio and others | drives that runtime's scheduler and drivers | The pattern is the same everywhere: 1. keep `fn main()` synchronous; 2. build the top-level future by calling your `async fn run()`; 3. hand it to `block_on`, which returns `run()`'s output. Because the future does not know which executor polls it, the same `run()` can be driven by different `block_on` functions, as long as nothing inside it depends on one runtime's I/O types. The code example drives one `run()` future type with futures' `block_on` and then with smol's, and both return the same count. Two details are worth saying out loud in an interview: - `block_on` **takes ownership** of the future and pins it internally, so you pass the future by value and get its `Output` back, not a `Poll`; - `block_on` is **itself synchronous**: it blocks the thread that calls it until the future is done, which is exactly what a synchronous `main` wants and exactly what async code must never do. ## Attribute macros are sugar over the same idea Tokio's `#[tokio::main]` lets you write `async fn main` again, but only because the macro rewrites it into a synchronous `main` that builds a runtime and calls its `block_on`; how that runtime is configured belongs with Tokio itself. smol keeps the macro out of its main crate and points to a separate `smol-macros` crate, so with smol the explicit `fn main() { smol::block_on(...) }` is the common form. ## Where `block_on` belongs and where it does not - **At the edges**: `main`, a test, a synchronous callback or a CLI command handler that needs one async result. - **Never inside async code**: calling `block_on` from within a future that an executor is already polling blocks that executor's thread; `futures::executor::block_on` detects the nesting and panics. Inside async code the answer is always `.await`. - **Not as a performance tool**: `block_on` runs the future on the calling thread; concurrency inside it still comes from `join!`-style combinators or from spawning tasks onto an executor. The short version for an interview: Rust deliberately leaves the runtime to libraries, so you choose one, call its `block_on` from synchronous `main`, and use `.await` everywhere else.
- What does `futures::executor::block_on` actually do while the future it is driving returns `Poll::Pending`?It parks the calling thread with `std::thread::park`. The `Waker` it put into the `Context` unparks that thread when called, and `block_on` polls again. It runs no I/O reactor of its own, so it only finishes futures whose wake-ups come from somewhere else, such as another thread, a channel or a runtime-independent reactor.
- Can a Rust program use `.await` directly inside `fn main` once a runtime crate is in `Cargo.toml`?No. Adding a crate does not change the language rule: `.await` outside an `async` function or block is error E0728. You either call that runtime's `block_on` from a synchronous `main`, or use an attribute macro such as `#[tokio::main]` that rewrites `main` into exactly that.
- Saying std ships a default executor that runs async main automatically.
- Believing adding a runtime crate makes `.await` legal inside a plain fn main.
- Calling block_on inside an async fn instead of awaiting the future.
- Thinking an async fn starts running as soon as it is called.
- Claiming a pure future written for one block_on cannot run under another.