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 futures;
extern crate tokio;
extern crate tokio_io;

use futures::{Future, Stream};
use tokio::executor::current_thread;
use tokio::net::TcpListener;
use tokio_io::{io, AsyncRead};

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
        current_thread::spawn(conn);

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

    // Spin up the server on the event loop
    current_thread::run(|_| {
        current_thread::spawn(server);
    });
}