mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-09-29 02:03:29 +00:00
async1: Read input from files
This commit is contained in:
committed by
Mo Bitar
parent
38a1078c23
commit
8cc9990af9
@@ -4,39 +4,38 @@
|
||||
// together, they can finish the job much faster.
|
||||
//
|
||||
// Let's simulate this using asynchronous programming. Each person is
|
||||
// represented as an asynchronous task, which can be executed concurrently (i.e.
|
||||
// they can be doing the calculations at the same time).
|
||||
|
||||
fn main() {
|
||||
// Async tasks need to be executed by a "runtime", which is not provided by
|
||||
// Rust's standard library. Here, we use the mainstream runtime `tokio`.
|
||||
let rt = tokio::runtime::Builder::new_current_thread()
|
||||
.build()
|
||||
.unwrap();
|
||||
|
||||
let scores_class_a = &[83, 77, 92];
|
||||
let scores_class_b = &[84, 88, 96];
|
||||
let scores_class_c = &[71, 83, 76];
|
||||
// represented as an asynchronous task, which can be executed concurrently.
|
||||
|
||||
// Async tasks need to be executed by a "runtime", which is not provided by
|
||||
// Rust's standard library. Here, we use the mainstream runtime `tokio`.
|
||||
// The macro `tokio::main` wraps the entire main function in a runtime.
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// TODO: Fix the compiler errors by making the spawned function async.
|
||||
let alice = rt.spawn(calculate_mean_score(scores_class_a));
|
||||
let bob = rt.spawn(calculate_mean_score(scores_class_b));
|
||||
let catherine = rt.spawn(calculate_mean_score(scores_class_c));
|
||||
let mean_score_a = tokio::spawn(calculate_mean_score("input_files/scores_class_a.txt"));
|
||||
let mean_score_b = tokio::spawn(calculate_mean_score("input_files/scores_class_b.txt"));
|
||||
let mean_score_c = tokio::spawn(calculate_mean_score("input_files/scores_class_c.txt"));
|
||||
|
||||
// Block the runtime on a task that awaits all three calculations.
|
||||
let [mean_score_a, mean_score_b, mean_score_c]: [usize; _] = rt.block_on(async {
|
||||
[
|
||||
// TODO: "await" all three tasks to fix the compiler error.
|
||||
alice, bob, catherine,
|
||||
]
|
||||
});
|
||||
|
||||
assert_eq!(mean_score_a, 84);
|
||||
assert_eq!(mean_score_b, 89);
|
||||
assert_eq!(mean_score_c, 76);
|
||||
// TODO: Await the spawned tasks to check their results.
|
||||
assert_eq!(mean_score_a, 84); // alice
|
||||
assert_eq!(mean_score_b, 89); // bob
|
||||
assert_eq!(mean_score_c, 76); // catherine
|
||||
}
|
||||
|
||||
fn calculate_mean_score(score_list: &[usize]) -> usize {
|
||||
let score_sum: usize = score_list.iter().sum();
|
||||
score_sum / score_list.len()
|
||||
// TODO: Fix the compiler errors by making the spawned function async.
|
||||
fn calculate_mean_score(scores_file: &str) -> usize {
|
||||
// Read the file asynchronously
|
||||
let file = tokio::fs::read_to_string(scores_file).await.unwrap();
|
||||
|
||||
// Initialize the sum and the number of scores
|
||||
let mut sum = 0;
|
||||
let mut n = 0;
|
||||
for line in file.lines() {
|
||||
// Parse every line as a score
|
||||
let score = line.parse::<usize>().unwrap();
|
||||
sum += score;
|
||||
n += 1;
|
||||
}
|
||||
|
||||
sum / n
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user