diff options
Diffstat (limited to 'rustlings/exercises/11_hashmaps')
| -rw-r--r-- | rustlings/exercises/11_hashmaps/hashmaps1.rs | 5 | ||||
| -rw-r--r-- | rustlings/exercises/11_hashmaps/hashmaps2.rs | 8 | ||||
| -rw-r--r-- | rustlings/exercises/11_hashmaps/hashmaps3.rs | 7 |
3 files changed, 16 insertions, 4 deletions
diff --git a/rustlings/exercises/11_hashmaps/hashmaps1.rs b/rustlings/exercises/11_hashmaps/hashmaps1.rs index 74001d0..a746b32 100644 --- a/rustlings/exercises/11_hashmaps/hashmaps1.rs +++ b/rustlings/exercises/11_hashmaps/hashmaps1.rs @@ -8,12 +8,15 @@ use std::collections::HashMap; fn fruit_basket() -> HashMap<String, u32> { // TODO: Declare the hash map. - // let mut basket = + 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. + basket.insert(String::from("banana"), 1); + basket.insert(String::from("orange"), 140000); + basket.insert(String::from("strawberry"), 5); basket } diff --git a/rustlings/exercises/11_hashmaps/hashmaps2.rs b/rustlings/exercises/11_hashmaps/hashmaps2.rs index e9f53fe..482db7c 100644 --- a/rustlings/exercises/11_hashmaps/hashmaps2.rs +++ b/rustlings/exercises/11_hashmaps/hashmaps2.rs @@ -29,9 +29,11 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) { ]; 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! + // This works just fine, but clippy doesn't like how readable and obvious the code is + // if !basket.contains_key(&fruit) { + // basket.insert(fruit, 10); + // } + basket.entry(fruit).or_insert(10); } } diff --git a/rustlings/exercises/11_hashmaps/hashmaps3.rs b/rustlings/exercises/11_hashmaps/hashmaps3.rs index 5b390ab..0fa4a6b 100644 --- a/rustlings/exercises/11_hashmaps/hashmaps3.rs +++ b/rustlings/exercises/11_hashmaps/hashmaps3.rs @@ -31,6 +31,13 @@ fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> { // Keep in mind that goals scored by team 1 will be the number of goals // conceded by team 2. Similarly, goals scored by team 2 will be the // number of goals conceded by team 1. + let team_1 = scores.entry(team_1_name).or_default(); + team_1.goals_scored += team_1_score; + team_1.goals_conceded += team_2_score; + let team_2 = scores.entry(team_2_name).or_default(); + team_2.goals_scored += team_2_score; + team_2.goals_conceded += team_1_score; + } scores |