mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-05-15 17:58:44 +00:00
The goal here was to get the first bit of "muscle memory" for using the async and await keywords. The little story should make it more intuitive for users why asynchronous programming is needed in the first place. This exercise will be moved to the location corresponding to the book in a later commit, to keep the diff of this one clean.
54 lines
1.6 KiB
Rust
54 lines
1.6 KiB
Rust
// Tim has to complete a few chores today, before he's allowed to play soccer
|
|
// with his friends. His friends decide to help him. Working together, they
|
|
// finish the chores earlier and have more time left to play soccer.
|
|
//
|
|
// Let's simulate this using asynchronous programming. Each boy is represented
|
|
// as an asynchronous task, which can be executed concurrently (they can be
|
|
// working at the same time).
|
|
|
|
use std::sync::atomic::{AtomicU8, Ordering};
|
|
|
|
// Used by "mom" to check that all chores are done before Tim plays soccer :-)
|
|
static CHORES_DONE: AtomicU8 = AtomicU8::new(0);
|
|
|
|
fn main() {
|
|
// Async tasks need to be executed by a "runtime", which is not provided by
|
|
// Rust's standard library. We use the popular "tokio" runtime here.
|
|
let rt = tokio::runtime::Builder::new_current_thread()
|
|
.build()
|
|
.unwrap();
|
|
|
|
let task_tim = rt.spawn(tim());
|
|
let task_carl = rt.spawn(carl());
|
|
let task_nick = rt.spawn(nick());
|
|
|
|
// Block the runtime on a task that waits for all boys to finish the chores.
|
|
rt.block_on(async {
|
|
task_tim.await.unwrap();
|
|
task_carl.await.unwrap();
|
|
task_nick.await.unwrap();
|
|
});
|
|
|
|
assert_eq!(
|
|
CHORES_DONE.load(Ordering::SeqCst),
|
|
3,
|
|
"Did you (a)wait for all the boys to finish the chores?"
|
|
);
|
|
println!("Ready to play soccer!");
|
|
}
|
|
|
|
async fn tim() {
|
|
println!("Cleaning my room...");
|
|
CHORES_DONE.fetch_add(1, Ordering::SeqCst);
|
|
}
|
|
|
|
async fn carl() {
|
|
println!("Washing the dishes...");
|
|
CHORES_DONE.fetch_add(1, Ordering::SeqCst);
|
|
}
|
|
|
|
async fn nick() {
|
|
println!("Mowing the lawn...");
|
|
CHORES_DONE.fetch_add(1, Ordering::SeqCst);
|
|
}
|