Convert Figma logo to code with AI

tokio-rs logotokio

A runtime for writing reliable asynchronous applications with Rust. Provides I/O, networking, scheduling, timers, ...

26,366
2,424
26,366
304

Top Related Projects

Async version of the Rust standard library

8,565

Actor framework for Rust.

14,343

An HTTP library for Rust

3,448

async fn(Request) -> Result<Response, Error>

Zero-cost asynchronous programming in Rust

3,613

A small and fast async runtime for Rust

Quick Overview

Tokio is a popular asynchronous runtime for Rust, providing essential building blocks for writing reliable and efficient network applications. It offers a rich set of tools for working with asynchronous I/O, timers, and concurrency, making it easier to develop scalable and performant systems.

Pros

  • Highly performant and efficient, leveraging Rust's zero-cost abstractions
  • Comprehensive ecosystem with a wide range of compatible libraries
  • Well-documented with extensive guides and examples
  • Active community and regular updates

Cons

  • Steep learning curve, especially for developers new to asynchronous programming
  • Can be complex to debug due to its asynchronous nature
  • Occasional breaking changes between major versions
  • May be overkill for simple, synchronous applications

Code Examples

  1. Basic TCP echo server:
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];
            
            loop {
                let n = match socket.read(&mut buf).await {
                    Ok(n) if n == 0 => return,
                    Ok(n) => n,
                    Err(_) => return,
                };

                if let Err(_) = socket.write_all(&buf[0..n]).await {
                    return;
                }
            }
        });
    }
}
  1. Asynchronous timer example:
use tokio::time::{sleep, Duration};

#[tokio::main]
async fn main() {
    println!("Start");
    sleep(Duration::from_secs(2)).await;
    println!("2 seconds have passed");
}
  1. Concurrent task execution:
use tokio::task;

#[tokio::main]
async fn main() {
    let handle = task::spawn(async {
        // Some async work
        "Task completed"
    });

    let result = handle.await.unwrap();
    println!("{}", result);
}

Getting Started

To use Tokio in your Rust project, add the following to your Cargo.toml:

[dependencies]
tokio = { version = "1.28", features = ["full"] }

Then, in your Rust file:

use tokio;

#[tokio::main]
async fn main() {
    println!("Hello, asynchronous world!");
}

Run your project with cargo run to start using Tokio.

Competitor Comparisons

Async version of the Rust standard library

Pros of async-std

  • Simpler API design, making it easier for beginners to get started
  • More closely follows Rust's standard library structure
  • Better support for WebAssembly (WASM) targets

Cons of async-std

  • Smaller ecosystem and fewer third-party integrations
  • Less mature and battle-tested in production environments
  • Fewer advanced features compared to Tokio

Code Comparison

async-std example:

use async_std::task;
use std::time::Duration;

async fn hello() {
    println!("Hello, world!");
}

fn main() {
    task::block_on(async {
        hello().await;
        task::sleep(Duration::from_secs(1)).await;
    });
}

Tokio example:

use tokio::time;

#[tokio::main]
async fn main() {
    println!("Hello, world!");
    time::sleep(time::Duration::from_secs(1)).await;
}

Both async-std and Tokio are popular asynchronous runtime libraries for Rust. async-std aims to provide a simpler, more familiar API that closely mirrors Rust's standard library. Tokio, on the other hand, offers a more comprehensive set of features and has a larger ecosystem. While async-std may be easier for beginners, Tokio is more widely adopted in production environments and offers better performance in many scenarios. The choice between the two depends on project requirements, developer experience, and specific use cases.

8,565

Actor framework for Rust.

Pros of Actix

  • Higher-level framework with built-in web server functionality
  • Generally faster performance for web applications
  • More opinionated, potentially leading to quicker development

Cons of Actix

  • Less flexible than Tokio for non-web async tasks
  • Steeper learning curve for beginners
  • Smaller ecosystem compared to Tokio

Code Comparison

Actix (web server example):

use actix_web::{web, App, HttpResponse, HttpServer};

async fn hello() -> HttpResponse {
    HttpResponse::Ok().body("Hello world!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().route("/", web::get().to(hello)))
        .bind("127.0.0.1:8080")?
        .run()
        .await
}

Tokio (basic async task):

use tokio;

#[tokio::main]
async fn main() {
    println!("Hello world!");
    tokio::time::sleep(std::time::Duration::from_secs(1)).await;
    println!("Goodbye world!");
}

Tokio is a more general-purpose async runtime, while Actix is specifically designed for web development. Tokio provides lower-level primitives for building async applications, offering more flexibility but requiring more code for web-specific tasks. Actix, built on top of Tokio, provides a higher-level framework tailored for web development, making it easier to create web applications but potentially less suitable for other types of async tasks.

14,343

An HTTP library for Rust

Pros of Hyper

  • Specialized for HTTP: Hyper is specifically designed for HTTP/1 and HTTP/2, making it more focused and optimized for web-related tasks
  • Higher-level abstraction: Provides a more user-friendly API for working with HTTP requests and responses
  • Smaller learning curve: Generally easier to get started with for developers primarily working on web applications

Cons of Hyper

  • Limited scope: Focused solely on HTTP, while Tokio offers a broader range of asynchronous I/O operations
  • Less flexibility: May not be suitable for non-HTTP network programming tasks that Tokio can handle
  • Dependency on Tokio: Hyper relies on Tokio for its asynchronous runtime, potentially limiting some customization options

Code Comparison

Tokio (basic TCP server):

let listener = TcpListener::bind("127.0.0.1:8080").await?;
loop {
    let (socket, _) = listener.accept().await?;
    tokio::spawn(async move { handle_connection(socket).await });
}

Hyper (basic HTTP server):

let addr = ([127, 0, 0, 1], 8080).into();
let service = make_service_fn(|_| async { Ok::<_, hyper::Error>(service_fn(hello_world)) });
let server = Server::bind(&addr).serve(service);
if let Err(e) = server.await { eprintln!("server error: {}", e); }
3,448

async fn(Request) -> Result<Response, Error>

Pros of Tower

  • Focused on middleware and service abstraction, providing a higher-level API for building robust network applications
  • Offers a modular approach, allowing developers to compose services and middleware easily
  • Provides a rich set of pre-built middleware components for common tasks like rate limiting, timeouts, and retries

Cons of Tower

  • More specialized and narrower in scope compared to Tokio's broader async runtime capabilities
  • Steeper learning curve for developers new to the concept of middleware and service-oriented architecture
  • Smaller community and ecosystem compared to Tokio

Code Comparison

Tower (service implementation):

use tower::{Service, ServiceBuilder};

struct MyService;

impl Service<Request> for MyService {
    type Response = Response;
    type Error = Error;
    type Future = impl Future<Output = Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: Request) -> Self::Future {
        async move {
            // Process request and return response
        }
    }
}

Tokio (async runtime usage):

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];

            // Read from the socket
            let n = socket.read(&mut buf).await.unwrap();

            // Write the data back
            socket.write_all(&buf[0..n]).await.unwrap();
        });
    }
}

Zero-cost asynchronous programming in Rust

Pros of futures-rs

  • More focused on providing core abstractions for asynchronous programming
  • Lighter weight and more modular, allowing for use in a wider range of projects
  • Offers a more flexible and customizable approach to futures and async/await

Cons of futures-rs

  • Less comprehensive ecosystem compared to Tokio
  • Requires more manual implementation of runtime features
  • May have a steeper learning curve for beginners in asynchronous Rust

Code Comparison

futures-rs:

use futures::executor::block_on;
use futures::future::Future;

async fn hello_world() -> String {
    "Hello, World!".to_string()
}

let future = hello_world();
let result = block_on(future);

Tokio:

use tokio;

#[tokio::main]
async fn main() {
    println!("Hello, World!");
}

Summary

futures-rs provides core abstractions for asynchronous programming in Rust, offering flexibility and modularity. It's well-suited for projects that require custom async implementations. Tokio, on the other hand, offers a more comprehensive ecosystem with built-in runtime features, making it easier for beginners to get started with asynchronous Rust programming. The choice between the two depends on the specific needs of your project and your familiarity with asynchronous programming concepts in Rust.

3,613

A small and fast async runtime for Rust

Pros of smol

  • Simpler and more lightweight, with a smaller codebase
  • Easier to understand and use for beginners
  • More flexible and composable, allowing for easier integration with other libraries

Cons of smol

  • Less feature-rich compared to Tokio
  • Smaller ecosystem and community support
  • May have lower performance in some scenarios due to its simplicity

Code Comparison

Tokio example:

#[tokio::main]
async fn main() {
    println!("Hello, world!");
}

smol example:

fn main() {
    smol::block_on(async {
        println!("Hello, world!");
    })
}

Both Tokio and smol are asynchronous runtime libraries for Rust, but they have different design philosophies. Tokio is a comprehensive, feature-rich framework with a larger ecosystem, while smol focuses on simplicity and composability. Tokio is generally preferred for large-scale production applications, while smol might be more suitable for smaller projects or when a lighter runtime is desired. The choice between the two depends on the specific requirements of your project, such as performance needs, ecosystem support, and complexity tolerance.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

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 MIT licensed Build Status Discord chat

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 sockets.

These components provide the runtime components necessary for building an asynchronous application.

Example

A basic TCP echo server with Tokio.

Make sure you activated the full features of the tokio crate on Cargo.toml:

[dependencies]
tokio = { version = "1.40.0", features = ["full"] }

Then, on your main.rs:

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(n) if n == 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;
                }
            }
        });
    }
}

More examples can be found here. For a larger "real world" example, see the mini-redis repository.

To see a list of the available features flags that can be enabled, check our docs.

Getting Help

First, see if the answer to your question can be found in the Guides or the API documentation. If the answer is not there, there is an active community in the Tokio Discord server. We would be happy to try to answer your question. You can also ask your question on the discussions page.

Contributing

:balloon: Thanks for your help improving the project! We are so happy to have you! We have a contributing guide to help you get involved in the Tokio project.

Related Projects

In addition to the crates in this repository, the Tokio project also maintains several other libraries, including:

  • axum: A web application framework that focuses on ergonomics and modularity.

  • hyper: A fast and correct HTTP/1.1 and HTTP/2 implementation for Rust.

  • tonic: A gRPC over HTTP/2 implementation focused on high performance, interoperability, and flexibility.

  • warp: A super-easy, composable, web server framework for warp speeds.

  • tower: A library of modular and reusable components for building robust networking clients and servers.

  • tracing (formerly tokio-trace): A framework for application-level tracing and async-aware diagnostics.

  • mio: A low-level, cross-platform abstraction over OS I/O APIs that powers tokio.

  • bytes: Utilities for working with bytes, including efficient byte buffers.

  • loom: A testing tool for concurrent Rust code.

Changelog

The Tokio repository contains multiple crates. Each crate has its own changelog.

Supported Rust Versions

Tokio will keep a rolling MSRV (minimum supported rust version) policy of at least 6 months. When increasing the MSRV, the new Rust version must have been released at least six months ago. The current MSRV is 1.70.

Note that the MSRV is not increased automatically, and only as part of a minor release. The MSRV history for past minor releases can be found below:

  • 1.39 to now - Rust 1.70
  • 1.30 to 1.38 - Rust 1.63
  • 1.27 to 1.29 - Rust 1.56
  • 1.17 to 1.26 - Rust 1.49
  • 1.15 to 1.16 - Rust 1.46
  • 1.0 to 1.14 - Rust 1.45

Note that although we try to avoid the situation where a dependency transitively increases the MSRV of Tokio, we do not guarantee that this does not happen. However, every minor release will have some set of versions of dependencies that works with the MSRV of that minor release.

Release schedule

Tokio doesn't follow a fixed release schedule, but we typically make one to two new minor releases each month. We make patch releases for bugfixes as necessary.

Bug patching policy

For the purposes of making patch releases with bugfixes, we have designated certain minor releases as LTS (long term support) releases. Whenever a bug warrants a patch release with a fix for the bug, it will be backported and released as a new patch release for each LTS minor version. Our current LTS releases are:

  • 1.32.x - LTS release until September 2024. (MSRV 1.63)
  • 1.36.x - LTS release until March 2025. (MSRV 1.63)
  • 1.38.x - LTS release until July 2025. (MSRV 1.63)

Each LTS release will continue to receive backported fixes for at least a year. If you wish to use a fixed minor release in your project, we recommend that you use an LTS release.

To use a fixed minor version, you can specify the version with a tilde. For example, to specify that you wish to use the newest 1.32.x patch release, you can use the following dependency specification:

tokio = { version = "~1.32", features = [...] }

Previous LTS releases

  • 1.8.x - LTS release until February 2022.
  • 1.14.x - LTS release until June 2022.
  • 1.18.x - LTS release until June 2023.
  • 1.20.x - LTS release until September 2023.
  • 1.25.x - LTS release until March 2024.

License

This project is licensed under the MIT license.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in Tokio by you, shall be licensed as MIT, without any additional terms or conditions.