Your First async fn
What a Future actually is, and why nothing happens until you await.
cargo add tokio --features full
cargo add reqwest --features json
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let body = reqwest::get("https://www.rust-lang.org").await?.text().await?;
println!("{} bytes", body.len());
Ok(())
}
The key idea: an async fn returns a Future, and a Future does nothing until it is
polled. Calling fetch() without .await builds a value and drops it โ no request is
made. Rust will warn you (unused implementer of Future), and you should believe the warning.
#[tokio::main] is a macro that wraps your body in a runtime. Without a runtime there is
nothing to poll the futures.