|
4 | 4 | // together, they can finish the job much faster. |
5 | 5 | // |
6 | 6 | // Let's simulate this using asynchronous programming. Each person is |
7 | | -// represented as an asynchronous task, which can be executed concurrently (i.e. |
8 | | -// they can be doing the calculations at the same time). |
9 | | - |
10 | | -fn main() { |
11 | | - // Async tasks need to be executed by a "runtime", which is not provided by |
12 | | - // Rust's standard library. Here, we use the mainstream runtime `tokio`. |
13 | | - let rt = tokio::runtime::Builder::new_current_thread() |
14 | | - .build() |
15 | | - .unwrap(); |
16 | | - |
17 | | - let scores_class_a = &[83, 77, 92]; |
18 | | - let scores_class_b = &[84, 88, 96]; |
19 | | - let scores_class_c = &[71, 83, 76]; |
| 7 | +// represented as an asynchronous task, which can be executed concurrently. |
20 | 8 |
|
| 9 | +// Async tasks need to be executed by a "runtime", which is not provided by |
| 10 | +// Rust's standard library. Here, we use the mainstream runtime `tokio`. |
| 11 | +// The macro `tokio::main` wraps the entire main function in a runtime. |
| 12 | +#[tokio::main] |
| 13 | +async fn main() { |
21 | 14 | // TODO: Fix the compiler errors by making the spawned function async. |
22 | | - let alice = rt.spawn(calculate_mean_score(scores_class_a)); |
23 | | - let bob = rt.spawn(calculate_mean_score(scores_class_b)); |
24 | | - let catherine = rt.spawn(calculate_mean_score(scores_class_c)); |
25 | | - |
26 | | - // Block the runtime on a task that awaits all three calculations. |
27 | | - let [mean_score_a, mean_score_b, mean_score_c]: [usize; _] = rt.block_on(async { |
28 | | - [ |
29 | | - // TODO: "await" all three tasks to fix the compiler error. |
30 | | - alice, bob, catherine, |
31 | | - ] |
32 | | - }); |
| 15 | + let mean_score_a = tokio::spawn(calculate_mean_score("input_files/scores_class_a.txt")); |
| 16 | + let mean_score_b = tokio::spawn(calculate_mean_score("input_files/scores_class_b.txt")); |
| 17 | + let mean_score_c = tokio::spawn(calculate_mean_score("input_files/scores_class_c.txt")); |
33 | 18 |
|
34 | | - assert_eq!(mean_score_a, 84); |
35 | | - assert_eq!(mean_score_b, 89); |
36 | | - assert_eq!(mean_score_c, 76); |
| 19 | + // TODO: Await the spawned tasks to check their results. |
| 20 | + assert_eq!(mean_score_a, 84); // alice |
| 21 | + assert_eq!(mean_score_b, 89); // bob |
| 22 | + assert_eq!(mean_score_c, 76); // catherine |
37 | 23 | } |
38 | 24 |
|
39 | | -fn calculate_mean_score(score_list: &[usize]) -> usize { |
40 | | - let score_sum: usize = score_list.iter().sum(); |
41 | | - score_sum / score_list.len() |
| 25 | +// TODO: Fix the compiler errors by making the spawned function async. |
| 26 | +fn calculate_mean_score(scores_file: &str) -> usize { |
| 27 | + // Read the file asynchronously |
| 28 | + let file = tokio::fs::read_to_string(scores_file).await.unwrap(); |
| 29 | + |
| 30 | + // Initialize the sum and the number of scores |
| 31 | + let mut sum = 0; |
| 32 | + let mut n = 0; |
| 33 | + for line in file.lines() { |
| 34 | + // Parse every line as a score |
| 35 | + let score = line.parse::<usize>().unwrap(); |
| 36 | + sum += score; |
| 37 | + n += 1; |
| 38 | + } |
| 39 | + |
| 40 | + sum / n |
42 | 41 | } |
0 commit comments