mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-10 12:49:18 +00:00
Complete hashmaps exercises
This commit is contained in:
parent
a6a9c0e30d
commit
65d5fdf7d1
@ -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<String, u32> {
|
||||
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
|
||||
}
|
||||
|
||||
@ -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<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!
|
||||
|
||||
basket.entry(fruit).or_insert(2);
|
||||
|
||||
// Bad way
|
||||
// match basket.get(&fruit) {
|
||||
// None => {
|
||||
// basket.insert(fruit, 2);
|
||||
// }
|
||||
// _ => {}
|
||||
// };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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<String, Team> {
|
||||
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
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user