diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index fd8dd2f8..1050f60d 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -10,15 +10,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(); // Two bananas are already given for you :) basket.insert(String::from("banana"), 2); + basket.insert(String::from("apple"), 0); + basket.insert(String::from("mango"), 3); // TODO: Put more fruits in your basket here. diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index a4f069a8..9385a663 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -13,8 +13,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)] @@ -39,6 +37,7 @@ fn fruit_basket(basket: &mut HashMap) { // 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/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index ad3baa68..70dfc385 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/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 team name and its goal details. @@ -40,6 +38,28 @@ 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) + .and_modify(|t| { + t.goals_scored += team_1_score; + t.goals_conceded += team_2_score; + }) + .or_insert_with_key(|team_name| Team { + name: team_name.to_string(), + 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_with_key(|team_name| Team { + name: team_name.to_string(), + goals_scored: team_2_score, + goals_conceded: team_1_score, + }); } scores } diff --git a/temp_15636_ThreadId1.exe b/temp_15636_ThreadId1.exe new file mode 100644 index 00000000..10d76e9f Binary files /dev/null and b/temp_15636_ThreadId1.exe differ