mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-09-28 17:53:28 +00:00
Add exercise async1
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.
This commit is contained in:
committed by
Mo Bitar
parent
e39e00e056
commit
fbb98ad4ea
@@ -0,0 +1,55 @@
|
||||
// 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();
|
||||
|
||||
// TODO: Fix the compiler errors by making the spawned function async.
|
||||
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.
|
||||
// TODO: "await" all three tasks to fix the compiler errors.
|
||||
rt.block_on(async {
|
||||
task_tim;
|
||||
task_carl;
|
||||
task_nick;
|
||||
});
|
||||
|
||||
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!");
|
||||
}
|
||||
|
||||
fn tim() {
|
||||
println!("Cleaning my room...");
|
||||
CHORES_DONE.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
fn carl() {
|
||||
println!("Washing the dishes...");
|
||||
CHORES_DONE.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
|
||||
fn nick() {
|
||||
println!("Mowing the lawn...");
|
||||
CHORES_DONE.fetch_add(1, Ordering::SeqCst);
|
||||
}
|
||||
Reference in New Issue
Block a user