README.md
1# Tokio
2
3A runtime for writing reliable, asynchronous, and slim applications with
4the Rust programming language. It is:
5
6* **Fast**: Tokio's zero-cost abstractions give you bare-metal
7 performance.
8
9* **Reliable**: Tokio leverages Rust's ownership, type system, and
10 concurrency model to reduce bugs and ensure thread safety.
11
12* **Scalable**: Tokio has a minimal footprint, and handles backpressure
13 and cancellation naturally.
14
15[![Crates.io][crates-badge]][crates-url]
16[![MIT licensed][mit-badge]][mit-url]
17[![Build Status][actions-badge]][actions-url]
18[![Discord chat][discord-badge]][discord-url]
19
20[crates-badge]: https://img.shields.io/crates/v/tokio.svg
21[crates-url]: https://crates.io/crates/tokio
22[mit-badge]: https://img.shields.io/badge/license-MIT-blue.svg
23[mit-url]: https://github.com/tokio-rs/tokio/blob/master/LICENSE
24[actions-badge]: https://github.com/tokio-rs/tokio/workflows/CI/badge.svg
25[actions-url]: https://github.com/tokio-rs/tokio/actions?query=workflow%3ACI+branch%3Amaster
26[discord-badge]: https://img.shields.io/discord/500028886025895936.svg?logo=discord&style=flat-square
27[discord-url]: https://discord.gg/tokio
28
29[Website](https://tokio.rs) |
30[Guides](https://tokio.rs/tokio/tutorial) |
31[API Docs](https://docs.rs/tokio/latest/tokio) |
32[Chat](https://discord.gg/tokio)
33
34## Overview
35
36Tokio is an event-driven, non-blocking I/O platform for writing
37asynchronous applications with the Rust programming language. At a high
38level, it provides a few major components:
39
40* A multithreaded, work-stealing based task [scheduler].
41* A reactor backed by the operating system's event queue (epoll, kqueue,
42 IOCP, etc...).
43* Asynchronous [TCP and UDP][net] sockets.
44
45These components provide the runtime components necessary for building
46an asynchronous application.
47
48[net]: https://docs.rs/tokio/latest/tokio/net/index.html
49[scheduler]: https://docs.rs/tokio/latest/tokio/runtime/index.html
50
51## Example
52
53A basic TCP echo server with Tokio:
54
55```rust,no_run
56use tokio::net::TcpListener;
57use tokio::io::{AsyncReadExt, AsyncWriteExt};
58
59#[tokio::main]
60async fn main() -> Result<(), Box<dyn std::error::Error>> {
61 let mut listener = TcpListener::bind("127.0.0.1:8080").await?;
62
63 loop {
64 let (mut socket, _) = listener.accept().await?;
65
66 tokio::spawn(async move {
67 let mut buf = [0; 1024];
68
69 // In a loop, read data from the socket and write the data back.
70 loop {
71 let n = match socket.read(&mut buf).await {
72 // socket closed
73 Ok(n) if n == 0 => return,
74 Ok(n) => n,
75 Err(e) => {
76 eprintln!("failed to read from socket; err = {:?}", e);
77 return;
78 }
79 };
80
81 // Write the data back
82 if let Err(e) = socket.write_all(&buf[0..n]).await {
83 eprintln!("failed to write to socket; err = {:?}", e);
84 return;
85 }
86 }
87 });
88 }
89}
90```
91
92More examples can be found [here][examples]. For a larger "real world" example, see the
93[mini-redis] repository.
94
95[examples]: https://github.com/tokio-rs/tokio/tree/master/examples
96[mini-redis]: https://github.com/tokio-rs/mini-redis/
97
98To see a list of the available features flags that can be enabled, check our
99[docs][feature-flag-docs].
100
101## Getting Help
102
103First, see if the answer to your question can be found in the [Guides] or the
104[API documentation]. If the answer is not there, there is an active community in
105the [Tokio Discord server][chat]. We would be happy to try to answer your
106question. You can also ask your question on [the discussions page][discussions].
107
108[Guides]: https://tokio.rs/tokio/tutorial
109[API documentation]: https://docs.rs/tokio/latest/tokio
110[chat]: https://discord.gg/tokio
111[discussions]: https://github.com/tokio-rs/tokio/discussions
112[feature-flag-docs]: https://docs.rs/tokio/#feature-flags
113
114## Contributing
115
116:balloon: Thanks for your help improving the project! We are so happy to have
117you! We have a [contributing guide][guide] to help you get involved in the Tokio
118project.
119
120[guide]: https://github.com/tokio-rs/tokio/blob/master/CONTRIBUTING.md
121
122## Related Projects
123
124In addition to the crates in this repository, the Tokio project also maintains
125several other libraries, including:
126
127* [`hyper`]: A fast and correct HTTP/1.1 and HTTP/2 implementation for Rust.
128
129* [`tonic`]: A gRPC over HTTP/2 implementation focused on high performance, interoperability, and flexibility.
130
131* [`warp`]: A super-easy, composable, web server framework for warp speeds.
132
133* [`tower`]: A library of modular and reusable components for building robust networking clients and servers.
134
135* [`tracing`]: A framework for application-level tracing and async-aware diagnostics.
136
137* [`rdbc`]: A Rust database connectivity library for MySQL, Postgres and SQLite.
138
139* [`mio`]: A low-level, cross-platform abstraction over OS I/O APIs that powers
140 `tokio`.
141
142* [`bytes`]: Utilities for working with bytes, including efficient byte buffers.
143
144* [`loom`]: A testing tool for concurrent Rust code
145
146[`warp`]: https://github.com/seanmonstar/warp
147[`hyper`]: https://github.com/hyperium/hyper
148[`tonic`]: https://github.com/hyperium/tonic
149[`tower`]: https://github.com/tower-rs/tower
150[`loom`]: https://github.com/tokio-rs/loom
151[`rdbc`]: https://github.com/tokio-rs/rdbc
152[`tracing`]: https://github.com/tokio-rs/tracing
153[`mio`]: https://github.com/tokio-rs/mio
154[`bytes`]: https://github.com/tokio-rs/bytes
155
156## Supported Rust Versions
157
158Tokio is built against the latest stable release. The minimum supported version is 1.45.
159The current Tokio version is not guaranteed to build on Rust versions earlier than the
160minimum supported version.
161
162## License
163
164This project is licensed under the [MIT license].
165
166[MIT license]: https://github.com/tokio-rs/tokio/blob/master/LICENSE
167
168### Contribution
169
170Unless you explicitly state otherwise, any contribution intentionally submitted
171for inclusion in Tokio by you, shall be licensed as MIT, without any additional
172terms or conditions.
173