Completed hashmaps
This commit is contained in:
@@ -8,12 +8,14 @@ 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("Apple"), 12);
|
||||
basket.insert(String::from("Orange"), 12);
|
||||
|
||||
basket
|
||||
}
|
||||
|
||||
@@ -32,6 +32,17 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||
// 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::Pineapple).or_insert(6);
|
||||
// basket.entry(Fruit::Banana).or_insert(4);
|
||||
|
||||
// OR
|
||||
let quantity = match fruit {
|
||||
Fruit::Pineapple => 6,
|
||||
Fruit::Banana => 4,
|
||||
_ => continue,
|
||||
};
|
||||
basket.entry(fruit).or_insert(quantity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 scores1= scores.entry(team_1_name).or_insert(TeamScores { goals_scored: (0), goals_conceded: (0) });
|
||||
scores1.goals_scored += team_1_score;
|
||||
scores1.goals_conceded += team_2_score;
|
||||
|
||||
let scores2= scores.entry(team_2_name).or_insert(TeamScores { goals_scored: (0), goals_conceded: (0) });
|
||||
scores2.goals_scored += team_2_score;
|
||||
scores2.goals_conceded += team_1_score;
|
||||
}
|
||||
|
||||
scores
|
||||
|
||||
Reference in New Issue
Block a user