33,742
stars
+174+175+331
3,764
forks
–
watchers
–
prs
–
issues
–
releases
–
discussions
README
Tokio
A runtime for writing reliable, asynchronous, and slim applications with
the Rust programming language. It is:
Fast: Tokio's zero-cost abstractions give you bare-metal
performance.
Reliable: Tokio leverages Rust's ownership, type system, and
concurrency model to reduce bugs and ensure thread safety.
Scalable: Tokio has a minimal footprint, and handles backpressure
and cancellation naturally.
[![Crates.io][crates-badge]][crates-url]
[![MIT licensed][mit-badge]][mit-url]
[![Build Status][actions-badge]][actions-url]
[![Discord chat][discord-badge]][discord-url]
[crates-badge]: https://img.shields.io/crates/v/tokio.svg
[crates-url]: https://crates.io/crates/tokio
[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
[mit-url]: https://github.com/tokio-rs/tokio/blob/master/LICENSE
[actions-badge]: https://github.com/tokio-rs/tokio/workflows/CI/badge.svg
[actions-url]: https://github.com/tokio-rs/tokio/actions?query=workflow%3ACI+branch%3Amaster
[discord-badge]: https://img.shields.io/discord/500028886025895936.svg?logo=discord&style=flat-square
[discord-url]: https://discord.gg/tokio
Website |
Guides |
API Docs |
Chat
Overview
Tokio is an event-driven, non-blocking I/O platform for writing
asynchronous applications with the Rust programming language. At a high
level, it provides a few major components:
A multithreaded, work-stealing based task [scheduler].
A reactor backed by the operating system's event queue (epoll, kqueue,
IOCP, etc.).
Asynchronous [TCP and UDP][net] sockets.
These components provide the runtime components necessary for building
an asynchronous application.
[net]: https://docs.rs/tokio/latest/tokio/net/index.html
[scheduler]: https://docs.rs/tokio/latest/tokio/runtime/index.html
Example
A basic TCP echo server with Tokio.
Make sure you enable the full features of the tokio crate on Cargo.toml:
toml
[dependencies]
tokio = { version = "1.54.1", features = ["full"] }
Then, on your main.rs:
rust,no_run
use tokio::net::TcpListener;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
[tokio::main]
async fn main() - Result {
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;
}
相关报道 (5)
News & coverage (11)
Hacker NewsTokio: Runtime for writing reliable asynchronous applications with Rust▲ 143 · 58 comments · 2020-04-26Hacker NewsTokio-Uring Design Proposal▲ 32 · 4 comments · 2021-04-08Hacker NewsThe design of Tokio-uring: Linux io_uring support for Rust▲ 4 · 2 comments · 2024-10-03Hacker NewsTokio-Uring Design Proposal - A new async Rust runtime▲ 4 · 2 comments · 2021-04-08Hacker NewsTokio: A runtime for writing reliable asynchronous applications with Rust▲ 2 · 0 comments · 2024-09-07news.lavx.huTokio releases Topcoat: A batteries-included full-stack Rust framework2026-11-09Amazon Web Services (AWS)Introducing Infinity: a runtime for extreme agent concurrency | Amazon Web Services2026-10-23Cloudflare BlogAsync QUIC and HTTP:3 made easy- tokio-quiche is now open-source2025-11-06RescanaTARmageddon (CVE-2025-62518): Critical RCE Vulnerability in Rust async-tar and tokio-tar Libraries Threatens Software Supply Chains2025-10-23DevClassTokio lead previews Toasty ORM – because Rust is productive for more than just systems programming2024-10-25