Introduction to Tokio
async tasks for you.In this page:
Adding Tokio as a Dependency
Using Tokio starts with adding it to Cargo.toml as an external dependency, since it is not part of the Rust standard library and must be fetched from crates.io.
Example: Adding Tokio as a Dependency
[dependencies]
tokio = { version = "1", features = ["full"] }
⚠️ Run this command in your terminal.
The #[tokio::main] Attribute
The #[tokio::main] macro is placed above async fn main(), transforming it into a real program entry point by setting up Tokio's async runtime behind the scenes. This snippet shows the shape of such code conceptually; it needs the tokio crate and network access to actually run.
Example: The #[tokio::main] Attribute
// Conceptual shape only -- requires the tokio crate (not available offline):
//
// #[tokio::main]
// async fn main() {
// let result = say_hello().await;
// println!("{}", result);
// }
//
// async fn say_hello() -> String {
// String::from("Hello from an async task")
// }
fn main() {
println!("This shows the shape of #[tokio::main] code without running it");
println!("Real Tokio programs need the crate fetched from crates.io");
}
Login to try C/C++/Java/PHP code in the editor
What Tokio Provides
Beyond just running futures, Tokio offers async-aware networking, timers, channels, and task spawning, all designed to work efficiently together under one cooperative scheduler.
Example: What Tokio Provides
fn main() {
let features = ["async TCP/UDP networking", "timers and sleep", "task spawning", "channels for message passing"];
for f in features.iter() {
println!("Tokio provides: {}", f);
}
}
Login to try C/C++/Java/PHP code in the editor
Why This Example Cannot Run Here
Because this course's code runner has no network access to download crates from crates.io, an actual Tokio-based program cannot be compiled or executed in this environment -- only plain standard-library Rust can run here.
Example: Why This Example Cannot Run Here
fn main() {
println!("Real async runtimes like Tokio require fetching an external crate");
println!("Without network access, only std-library-only Rust can compile and run here");
}
Login to try C/C++/Java/PHP code in the editor
- Assuming Tokio is part of the Rust standard library -- it is an external crate that must be added as a dependency via Cargo.
- Forgetting
#[tokio::main]is what transforms anasync fn main()into a regularfn main()that sets up and runs the runtime. - Trying to run networked or timer-based async examples without network access, which will not work in an offline sandbox like this course's code runner.
- Tokio is a widely used asynchronous runtime crate, added as an external dependency in
Cargo.toml. - The
#[tokio::main]attribute macro turns anasync fn main()into a runnable program by setting up Tokio's executor. - Tokio provides async-friendly versions of I/O, timers, and task spawning built around the
Futuretrait. - Because Tokio must be downloaded from crates.io, it requires network access and cannot run in a fully offline sandbox.
Chapter Quiz — Complete all 6 topics to unlock
0/6 topics done
Complete these topics first: