Member-only story
A Rust Async Primer-Pt 3
Part 2 of this series used a trivial example to explain the basic principles and components of asynchronous Rust. In this one, we’ll demonstrate how to include a separate runtime crate and use it to run concurrent tasks, as well as give a brief comparison of the current options.
When we last left off we were using the primitive executor that is available with the futures crate, sometimes called futures-rs. The futures crate provides the common abstraction layer for async behavior in the Rust language. It is intended to be the basis for creating complex runtimes. As discussed in the previous articles, this is by design. Unless you intend to create a custom runtime, executors, etc., from scratch, you probably won’t be using futures directly.
I’ve chosen to use the async-std crate for this example simply because it’s easily read by anyone already familiar with the Rust std library. I’ll also be using timers to demonstrate that we’re actually running in a concurrent fashion as expected.
First, we’ll set up our cargo.toml file as follows.
[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]
async-std = "1.12.0"
futures = "0.3"
rand = "0.8.5"