Async Rust · 18 min · free lesson

Runtimes

Rust ships the async syntax but not the engine that drives it.

Core idea

std defines Future; the executor is a library decision.

Diagram for Runtimes: a Rust code example flows to the lesson rule and the key observation.
The runtime attribute is what actually polls the future to completion.

Understand it

The language defines Future and .await; polling, timers, and I/O readiness come from a runtime you choose, such as tokio or smol. That keeps async usable on an embedded target with no allocator, at the cost of one decision you have to make per project.

Rust's language and standard library define Future and .await, but a program needs an executor to poll futures and often a runtime to provide timers, sockets, and task spawning. Tokio is a common choice, but an async function is not inherently tied to one runtime. A library should be clear about whether it uses runtime-specific I/O types or exposes runtime-neutral futures. The decision affects how easily callers can integrate it.

The runtime schedules ready tasks; it cannot make blocking code magically nonblocking. A synchronous file operation or long CPU loop inside an async task can occupy an executor worker and delay unrelated tasks. Use the runtime's dedicated blocking facility for genuinely blocking work, and measure when offloading is worthwhile. Also know who owns spawned tasks: dropping a join handle does not necessarily cancel the task, depending on the runtime API. Cancellation and shutdown behavior belong in the program's design.

Create a minimal program with an async entry point and an awaited timer. Then remove the runtime wrapper and inspect the compiler or execution setup needed to drive the future. Add a second task and observe that scheduling depends on await points. When choosing a runtime for a project, check required I/O, platform support, footprint, and shutdown semantics. For embedded or restricted targets, the executor can be much smaller than a server runtime, but you still need a concrete way to poll and wake futures.

An executor and an operating-system event source solve different problems: one chooses which ready future to poll, while the other reports that I/O is ready. A runtime often bundles both. This separation explains why creating a future is cheap but actually driving a network request requires runtime services.

Local Cargo profile: open the supplied project in Crabrix or the browser Playground. Explicitly prepare its pinned Cargo packages once on each device, then Run and Debug compile and execute on that device without a backend. The project includes Cargo.lock. The lesson question remains the completion exercise; running an unchanged demonstration does not prove a separate programming challenge. Tokio 1.54.1 is built with default-features=false and only rt/sync. The current-thread builder needs no runtime attribute macro. JoinSet, Semaphore, watch and yield_now are real Tokio APIs. This profile supplies cooperative concurrency, not native threads, sockets, OS signals or CPU parallelism.

Read the code

// [dependencies.tokio]
// version = "1"
// default-features = false
// features = ["rt"]
// No runtime attribute macro is needed.
fn main() {
    use tokio::runtime::Builder;
    let runtime = Builder::new_current_thread()
        .build()
        .unwrap();
    let value = runtime.block_on(compute());
    println!("{value}");
}
async fn compute() -> u32 {
    42
}

A synchronous main explicitly asks a current-thread executor to poll the future.

Open local example in Playground ↗

Try it

Describe what the runtime provides that the language does not. Open the supplied local Cargo project, prepare packages, then Run or Debug it. Change inputs and examine the assertions. Answer the lesson question separately.

What success looks like: You can name polling, waking, and I/O readiness as runtime concerns.

Check your understanding

Why is that not a valid program entry point?

Reveal the answer
  1. main is not allowed to print before awaiting
  2. main cannot be async without a runtime to poll it
  3. async fn is still unstable in this edition

Something has to drive the future, and that something is the runtime.

Go deeper

Common misconception: “async is built into Rust, so a plain async fn main just runs.”

std defines Future; the executor is a library decision. async fn main is rejected with E0752, because something outside the language has to poll the future.

  1. Find the entry point: #[tokio::main] rewrites async fn main into an ordinary fn main that starts a runtime and hands it the body.
  2. Poll the body: The runtime polls the body. compute().await finishes on its first poll with 42, so this task never has to wait.
  3. Remove the driver: Delete #[tokio::main] and the compiler reports E0752, because main may not be async without something to drive it.

Transfer challenge: Replace tokio with smol: keep compute unchanged and drive it with smol::block_on from a plain fn main. Note what changed in Cargo.toml and main, and what stayed the same.

Connections

  • Futures: async fn, await, and laziness
  • Async Patterns: join, select, and cancellation
Learn this lesson with AI

Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.

I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools.
Lesson: Runtimes (courseId "concurrency", lessonId "async-runtime")
Link: https://crabrix.com/learn/concurrency/async-runtime/
Goal: std defines Future; the executor is a library decision.
Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=concurrency/async-runtime) and I will write the code there.
Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.