From 9203b20a521d37feed97ae0788dca7b1b8624056 Mon Sep 17 00:00:00 2001 From: Rock070 Date: Sat, 30 Dec 2023 18:25:07 +0800 Subject: [PATCH] Refactor build_scores_table function to update goals scored and conceded --- exercises/11_hashmaps/hashmaps3.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/exercises/11_hashmaps/hashmaps3.rs b/exercises/11_hashmaps/hashmaps3.rs index 08e977c3..771eb8f9 100644 --- a/exercises/11_hashmaps/hashmaps3.rs +++ b/exercises/11_hashmaps/hashmaps3.rs @@ -14,8 +14,6 @@ // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::collections::HashMap; // A structure to store the goal details of a team. @@ -39,6 +37,25 @@ fn build_scores_table(results: String) -> HashMap { // will be the number of goals conceded from team_2, and similarly // goals scored by team_2 will be the number of goals conceded by // team_1. + scores.entry(team_1_name.clone()).or_insert(Team { + goals_scored: 0, + goals_conceded: 0 + }); + + scores.entry(team_2_name.clone()).or_insert(Team { + goals_scored: 0, + goals_conceded: 0 + }); + + if let Some(team) = scores.get_mut(&team_1_name) { + team.goals_scored += team_1_score; + team.goals_conceded += team_2_score; + } + + if let Some(team) = scores.get_mut(&team_2_name) { + team.goals_scored += team_2_score; + team.goals_conceded += team_1_score; + } } scores }