diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index fd8dd2f8..d9e7b0a8 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -10,17 +10,16 @@ // // 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); - // TODO: Put more fruits in your basket here. + basket.insert(String::from("cherry"), 2); + basket.insert(String::from("dragonfruit"), 2); basket } diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index a4f069a8..6bdf091d 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)] @@ -36,9 +34,18 @@ fn fruit_basket(basket: &mut HashMap) { ]; 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(2); + + // Bad way + // match basket.get(&fruit) { + // None => { + // basket.insert(fruit, 2); + // } + // _ => {} + // }; } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index ad3baa68..2847ae87 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. @@ -35,11 +33,28 @@ fn build_scores_table(results: String) -> HashMap { let team_1_score: u8 = v[2].parse().unwrap(); let team_2_name = v[1].to_string(); let team_2_score: u8 = v[3].parse().unwrap(); - // TODO: Populate the scores table with details extracted from the - // current line. Keep in mind that goals scored by team_1 - // 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. + + let team_1 = scores.entry(team_1_name.clone()).or_insert(Team { + name: team_1_name.clone(), + goals_scored: 0, + goals_conceded: 0, + }); + *team_1 = Team { + name: team_1_name.clone(), + goals_scored: team_1.goals_scored + team_1_score, + goals_conceded: team_1.goals_conceded + team_2_score, + }; + + let team_2 = scores.entry(team_2_name.clone()).or_insert(Team { + name: team_2_name.clone(), + goals_scored: 0, + goals_conceded: 0, + }); + *team_2 = Team { + name: team_2_name.clone(), + goals_scored: team_2.goals_scored + team_2_score, + goals_conceded: team_2.goals_conceded + team_1_score, + }; } scores }