Refactor build_scores_table function to update goals scored and conceded

This commit is contained in:
Rock070 2023-12-30 18:25:07 +08:00
parent b42cfb167d
commit 9203b20a52

View File

@ -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<String, Team> {
// 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
}