Skip to content

Commit

Permalink
solution to hashmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
Matrx123 committed Apr 7, 2024
1 parent 74396a2 commit 918e4bb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
4 changes: 3 additions & 1 deletion exercises/11_hashmaps/hashmaps2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
// hint.

// I AM NOT DONE

use std::collections::HashMap;

#[derive(Debug)]
#[derive(Hash, PartialEq, Eq)]
enum Fruit {
Apple,
Expand All @@ -38,6 +38,8 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {

for fruit in fruit_kinds {
// Insert new fruits if they are not already present in the
//Solution
basket.entry(fruit).or_insert(1);

}
}
Expand Down
10 changes: 8 additions & 2 deletions exercises/11_hashmaps/hashmaps3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
// 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.
#[derive(Debug)]
struct Team {
goals_scored: u8,
goals_conceded: u8,
Expand All @@ -40,6 +40,13 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
// will be the number of goals conceded by team_2, and similarly
// goals scored by team_2 will be the number of goals conceded by
// team_1.
//Solution
let team1 = scores.entry(team_1_name).or_insert(Team { goals_scored: 0, goals_conceded: 0});
team1.goals_scored += team_1_score;
team1.goals_conceded += team_2_score;
let team2 = scores.entry(team_2_name).or_insert(Team {goals_scored: 0, goals_conceded: 0});
team2.goals_scored += team_2_score;
team2.goals_conceded += team_1_score;
}
scores
}
Expand All @@ -60,7 +67,6 @@ mod tests {
#[test]
fn build_scores() {
let scores = build_scores_table(get_results());

let mut keys: Vec<&String> = scores.keys().collect();
keys.sort();
assert_eq!(
Expand Down

0 comments on commit 918e4bb

Please sign in to comment.