A platform for writing fast networking code with Rust.

Get Started

Fast

Tokio's zero-cost abstractions give you bare-metal performance.

Productive

Tokio makes it easy to implement protocols and program asynchronously.

Reliable

Tokio leverages Rust's ownership and concurrency model to ensure thread safety.

Scalable

Tokio has a minimal footprint, and handles backpressure and cancellation naturally.

// A tiny async echo server with Tokio
extern crate tokio;

use tokio::io;
use tokio::net::TcpListener;
use tokio::prelude::*;

fn main() {
    // Bind the server's socket
    let addr = "127.0.0.1:12345".parse().unwrap();
    let tcp = TcpListener::bind(&addr).unwrap();

    // Iterate incoming connections
    let server = tcp.incoming().for_each(|tcp| {
        // Split up the read and write halves
        let (reader, writer) = tcp.split();

        // Copy the data back to the client
        let conn = io::copy(reader, writer)
            // print what happened
            .map(|(n, _, _)| {
                println!("wrote {} bytes", n)
            })
            // Handle any errors
            .map_err(|err| {
                println!("IO error {:?}", err)
            });

        // Spawn the future as a concurrent task
        tokio::spawn(conn);

        Ok(())
    })
    .map_err(|err| {
        println!("server error {:?}", err);
    });

    // Start the runtime and spin up the server
    tokio::run(server);
}