Member-only story
A Rust Async Primer-Pt 2
Basic async usage
This is part 2 in a 3 part series on understanding the basics of asynchronous programming in Rust. Part 1 focused on the concepts around asynchronous programming, and specifically, how Rust implements it. In this one, we’re going to look at some example code.
First, let’s create a new project using Cargo with cargo new rustasync --bin
Now, open up the generated Cargo.toml file and add the futures-rs crate to the dependencies section. You can follow the link above to learn more and find the complete documentation but in summary, it’s a bare-bones implementation of an async runtime. Your toml file should look similar to the one below:
[package]
name = "rust-async"
version = "0.1.0"
edition = "2021"# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html[dependencies]
futures = "0.3"
The futures-rs crate implements some of Rust's core abstractions for asynchronous programming. We’ll go over those in more depth later but for now, the Future
trait is of the most relevance to us.
Let’s imagine that we’re going to fix breakfast at home. We might choose to make some coffee, eggs, and because I’m trying to be healthy, use the oven to make some bacon. My process…