API Reference
Tokio
Asynchronous runtime for Rust providing I/O, timers, tasks, and synchronization primitives.
Official Documentation ↗CompatibilityThis documentation covers version
v1.40. Check the official docs for the latest API surface.Installation
cargo
cargo add tokioMethods
No methods match this filter.
Tokio is an asynchronous runtime for Rust, providing the building blocks needed for writing networking, scheduling, timers, and async I/O applications with reliable performance.
Key Features
- Runtime: Multi-threaded work-stealing executor
- Async I/O: TCP, UDP, filesystem, and signal handling
- Timers: Sleep, interval, and timeout futures
- Synchronization: Channels, mutexes, and oneshot
- select!: Race multiple futures concurrently
Errors
The official API reference declares the following error types:
tokio::task::JoinError(struct) — official text: “pub struct JoinError { /* private fields */ } Available on crate featurertonly. Task failed to execute to completion. pub fn is_cancelled(&self) -> bool Returns true if the error was caused by the task being cancelled. pub fn is_panic(&self) -> bool Return” (source: https://docs.rs/tokio/latest/tokio/task/struct.JoinError.html, fetched 2026-11-08T16:01:12.9137723+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:01:12.9137723+08:00 via WebFetch docs.rs tokio latest 的 fn.spawn、fn.sleep、mpsc::channel、macro.select、attr.main 与 JoinError 页.
#[tokio::main]→ signature from https://docs.rs/tokio/latest/tokio/attr.main.html; official text: “# Attribute Macro main #[main] Available on crate featuresmacrosandrtonly. Marks async function to be executed by the selected runtime. ## Usage ### Using the multi-threaded runtime #[tokio::main] async fn main()”tokio::spawn→ signature from https://docs.rs/tokio/latest/tokio/task/fn.spawn.html; official text: “pub fn spawn(future: F) -> JoinHandle<F::Output> where F: Future + Send + ’static, F::Output: Send + ’static, Spawns a new asynchronous task, returning a JoinHandlefor it.”tokio::select!→ signature from https://docs.rs/tokio/latest/tokio/macro.select.html; official text: “macro_rules! select { { $( biased; )? $( $bind:pat = $fut:expr $(, if $cond:expr)? => $handler:expr, )* $( else => $els:expr $(,)? )? } => { … }; }”tokio::time::sleep→ signature from https://docs.rs/tokio/latest/tokio/time/fn.sleep.html; official text: “pub fn sleep(duration: Duration) -> Sleep Available on crate featuretimeonly. Waits untildurationhas elapsed.”tokio::sync::mpsc→ signature from https://docs.rs/tokio/latest/tokio/sync/mpsc/fn.channel.html; official text: “pub fn channel(buffer: usize) -> (Sender , Receiver ) Available on crate feature synconly. Creates a bounded mpsc channel for communicating between asynchronous tasks with backpressure.”
FAQ
- What is the difference between current_thread and multi_thread runtime?
- current_thread runs all tasks on one thread with less overhead, ideal for I/O-bound work. multi_thread uses a thread pool to run CPU and I/O tasks in parallel. Choose multi_thread for mixed or CPU-heavy workloads.
- When should I use spawn versus join?
- Use tokio::spawn to run a task concurrently and detach it, collecting results via JoinHandle. Use join! to run futures concurrently on the current task and wait for all. Spawn is for background work; join is for bounded concurrent steps.
- How do I bridge sync and async code?
- Use tokio::task::spawn_blocking to run blocking sync code on a dedicated thread pool without stalling the async runtime. Never call blocking I/O directly in async code, as it blocks the executor thread.
Report a problem with this API docOpens a pre-filled issue on GitHub.