diff --git a/exercises/11_hashmaps/hashmaps1.rs b/exercises/11_hashmaps/hashmaps1.rs index 80829ea..d526e3d 100644 --- a/exercises/11_hashmaps/hashmaps1.rs +++ b/exercises/11_hashmaps/hashmaps1.rs @@ -11,16 +11,15 @@ // Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::collections::HashMap; fn fruit_basket() -> HashMap { - let mut basket = // TODO: declare your hash map here. + let mut basket = HashMap::new();// TODO: declare your hash map here. // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); - + basket.insert(String::from("peach"), 3); + basket.insert(String::from("apple"), 1); // TODO: Put more fruits in your basket here. basket diff --git a/exercises/11_hashmaps/hashmaps2.rs b/exercises/11_hashmaps/hashmaps2.rs index a592569..0e0cb57 100644 --- a/exercises/11_hashmaps/hashmaps2.rs +++ b/exercises/11_hashmaps/hashmaps2.rs @@ -14,8 +14,6 @@ // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::collections::HashMap; #[derive(Hash, PartialEq, Eq)] @@ -34,12 +32,14 @@ fn fruit_basket(basket: &mut HashMap) { Fruit::Mango, Fruit::Lychee, Fruit::Pineapple, - ]; + ]; for fruit in fruit_kinds { // TODO: Insert new fruits if they are not already present in the // basket. Note that you are not allowed to put any type of fruit that's // already present! + basket.entry(fruit).or_insert(1); + } } diff --git a/exercises/11_hashmaps/hashmaps3.rs b/exercises/11_hashmaps/hashmaps3.rs index 36544ee..800c4d9 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,18 @@ fn build_scores_table(results: String) -> HashMap { // 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. + scores.entry(team_1_name).and_modify( + |t|{ + t.goals_scored += team_1_score; + t.goals_conceded += team_2_score; + } + ).or_insert(Team{goals_scored: team_1_score, goals_conceded: team_2_score}); + scores.entry(team_2_name).and_modify( + |t|{ + t.goals_scored += team_2_score; + t.goals_conceded += team_1_score; + }).or_insert(Team{goals_scored: team_2_score, goals_conceded: team_1_score}); + } scores }