Asynchronous Programming with Tokio
If you are interested in learning about asynchronous programming in more depth, I recommend reading Asynchronous Programming in Rust.
Asynchronous programming
When we run code that makes network requests, these request are sent through the network.
Sending the request and waiting for the response is done by the network peripheral and doesn’t require the CPU. This means, the CPU is free to do other things while it waits.
Code written synchronously will send a request and then block the thread waiting for a response. For example:
1
2
3
4
fn main() {
let resp = reqwest::blocking::get("https://httpbin.org/ip")?.text()?;
println!("{:#?}", resp);
}