Go to content
Web development

Writing Async Rust Without Tokio

When you write asynchronous Rust without Tokio, your async functions still return futures, but another part of your program must run them. A future is a value that represents work that may finish later. An executor polls that future, which means it asks the future to make progress. The executor schedules the future again when it is ready to continue.

For a simple entry point, use a small executor. Choose another runtime such as smol if your program needs more services, or write a library that lets its caller choose the runtime. The right choice depends on what your program needs. block_on runs a future to completion, while network I/O, timers, and background tasks need additional support. Knowing the difference helps you avoid adding a runtime you do not need or expecting an executor to provide services it lacks.

Choose what will run your futures

Calling an async fn returns a future; it does not start the function’s work. An executor advances the future by polling it. An .await expression adds one future’s work to another, so the outer future pauses until the awaited work is ready. The Tokio async tutorial and the Rust Async Book explain this relationship.

For a small program that only needs to wait for a future to finish, futures-executor provides block_on, which runs a future to completion on the current thread. For example:

async fn calculate() -> u32 {
    2 + 2
}

fn main() {
    let result = futures_executor::block_on(calculate());
    println!("{result}");
}

This example runs a future, but it does not add support for asynchronous networking, timers, or background tasks. At the synchronous entry point, block_on drives the future and waits for it to finish. If the future needs a timer or I/O operation, your program needs a separate service to provide it. The futures-executor documentation lists block_on, LocalPool, and ThreadPool as executor options.

If your program needs networking, timers, and task spawning, use a runtime that supplies those services. smol offers an executor, async networking and filesystem interfaces, timers, channels, and block_on. Its documentation shows an HTTP client using smol::block_on to connect, write a request, and copy the response. That makes smol a practical alternative when you want a runtime but do not want Tokio.

Understand what an executor does

An executor polls tasks, which are futures it has taken responsibility for running. A future returns Poll::Ready(value) when it has finished, or Poll::Pending when it needs to wait. When a future returns Pending, its Waker lets it notify the executor when it is ready for another poll. The standard library’s Waker documentation explains that the executor chooses when to run a task after a wake, and that it can combine multiple wake calls into one.

Wake notifications affect whether futures finish and how much CPU the executor uses. If a future returns Pending and never gets woken, it can remain stuck. An executor that repeatedly polls every waiting future wastes CPU. A wake-driven executor waits for a notification before polling a task again. The Tokio tutorial’s MiniTokio example shows how a channel can schedule tasks.

A minimal executor helps you learn how polling and wakeups work, but it needs more than a loop around poll. The Async Book executor example puts futures in a task queue. A waker puts a task back in the queue when it needs more work. Use the example to learn, not as a production runtime. A production executor also needs to handle task shutdown, panics, and queue limits. The example does not provide a complete strategy for these cases.

Keep libraries independent of Tokio

If you are writing a library, accept standard Rust futures. Require a specific runtime only when your API needs services that runtime provides. The caller can then run your future with an executor they already use. If your library needs to start background tasks, schedule timers, or run blocking work, make that requirement part of the API or accept an executor abstraction. Do not assume Tokio is present without saying so.

An executor abstraction that works across runtimes can give callers common services, but it also limits which runtimes they can use. For example, agnostic_async_executor says it supports starting tasks and using timers across the executors it integrates with. Its README also says it does not automatically support other executors.

Check task bounds before choosing an executor. Send is a Rust marker trait for values that can move safely between threads. The smol Executor documentation requires spawned futures to be Send + 'a; smol also provides a single-threaded LocalExecutor for futures that do not need to be Send. Do not add Send bounds to a library API without a reason, since that can rule out futures that hold thread-local state.

A runtime that works with different executors is not automatically portable across operating systems. It still needs a way to wait until I/O is ready. Filesystem operations add another challenge: widely portable nonblocking filesystem operations are not generally available, so runtimes commonly use threads for blocking filesystem work. The Rust forum discussion on runtime-agnostic library design explains the difference between runtime portability and operating-system portability. If your library does file or CPU work that blocks, decide who provides the worker threads and tell callers what your library needs.

Pick the smallest option that fits

Use block_on when a synchronous entry point needs to run a future to completion and the libraries that future uses already provide the services it needs. Choose smol or another runtime when your program needs timers, networking, or concurrent tasks. Keep your library independent of a runtime when callers benefit from choosing their own executor, and document the services your library needs.

Write a custom executor to learn how polling and wakeups work, or when you have a narrow, controlled use case. If your service needs production scheduling and I/O, choose an existing runtime. A teaching executor is not a complete substitute.

More in Web development Back to top ↑