mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 21:29:18 +00:00
hashmaps
This commit is contained in:
parent
4f7dc061c6
commit
cc412f8756
@ -10,17 +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<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("apple"), 1);
|
||||
basket.insert(String::from("orange"), 2);
|
||||
|
||||
basket
|
||||
}
|
||||
|
||||
@ -11,8 +11,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)]
|
||||
@ -34,9 +32,9 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||
];
|
||||
|
||||
for fruit in fruit_kinds {
|
||||
// TODO: Put new fruits if not already present. Note that you
|
||||
// are not allowed to put any type of fruit that's already
|
||||
// present!
|
||||
if !basket.contains_key(&fruit) {
|
||||
basket.insert(fruit, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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,15 +33,24 @@ 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 number of goals conceded from team_2, and similarly
|
||||
// goals scored by team_2 will be the number of goals conceded by
|
||||
// team_1.
|
||||
|
||||
calc_score(&mut scores, team_1_name, team_1_score, team_2_score);
|
||||
calc_score(&mut scores, team_2_name, team_2_score, team_1_score);
|
||||
}
|
||||
scores
|
||||
}
|
||||
|
||||
|
||||
fn calc_score (scores: &mut HashMap<String, Team>, team_name: String, goals_scored: u8, goals_conceded: u8) {
|
||||
let mut team = scores.entry(team_name.clone()).or_insert(Team {
|
||||
name: team_name,
|
||||
goals_scored: 0,
|
||||
goals_conceded: 0
|
||||
});
|
||||
team.goals_scored += goals_scored;
|
||||
team.goals_conceded += goals_conceded;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user