tokio: A runtime for writing reliable, asynchronous, and slim applications with the Rust programming language.
What problem it solves
Writing asynchronous applications requires runtime components for scheduling tasks, reacting to I/O events, and handling network communication without blocking threads. Tokio provides these components for Rust so applications can coordinate concurrent I/O through an event-driven, non-blocking platform.
Key features
Task scheduling — The runtime includes a multithreaded, work-stealing task scheduler for distributing asynchronous work.
Source: README, tokio
Event-driven I/O — A reactor connects asynchronous applications to operating system event queues such as epoll, kqueue, and IOCP.
Source: README, tokio
Asynchronous networking — The platform provides asynchronous TCP and UDP sockets for network communication.
Source: README, tokio
Rust concurrency foundations — Tokio uses Rust's ownership, type system, and concurrency model to support thread safety and reduce bugs.
Source: README, tokio
Workspace of companion crates — The workspace includes stream, utility, macro, and testing crates alongside the main runtime crate.
Source: README, tokio-macros, tokio-stream
Quick start
Copied verbatim from the project's README.
[dependencies]
tokio = { version = "1.54.1", features = ["full"] }use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
let (mut socket, _) = listener.accept().await?;
tokio::spawn(async move {
let mut buf = [0; 1024];
// In a loop, read data from the socket and write the data back.
loop {
let n = match socket.read(&mut buf).await {
// socket closed
Ok(0) => return,
Ok(n) => n,
Err(e) => {
eprintln!("failed to read from socket; err = {:?}", e);
return;
}
};
// Write the data back
if let Err(e) = socket.write_all(&buf[0..n]).await {
eprintln!("failed to write to socket; err = {:?}", e);
return;
}
}
});
}
}Who it may suit
This may suit Rust developers building network services, concurrent I/O applications, or other programs that need an asynchronous runtime.
Topics to discuss
Which runtime configuration and feature-flag combinations are most useful for common application patterns?
How can developers structure larger asynchronous applications while using Tokio's scheduler and networking components?
What examples, diagnostics, or testing practices would help newcomers understand asynchronous Rust applications?
Written by ReadmeX AI from the project's README, source and public GitHub data; it may contain mistakes.
Read the original README https://github.com/tokio-rs/tokio#readme
ReadmeX opened this community for a trending project; it is not run by the project. Maintainers can claim it from the community page.
Comments
…