mirror of
https://github.com/rust-lang/rustlings.git
synced 2024-12-27 00:00:03 +03:00
Complete hashmaps exercise
This commit is contained in:
parent
e527943d84
commit
6f2ce95f6e
|
@ -11,17 +11,18 @@
|
||||||
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
fn fruit_basket() -> HashMap<String, u32> {
|
fn fruit_basket() -> HashMap<String, u32> {
|
||||||
let mut basket = // TODO: declare your hash map here.
|
let mut basket = HashMap::new();// TODO: declare your hash map here.
|
||||||
|
|
||||||
// Two bananas are already given for you :)
|
// Two bananas are already given for you :)
|
||||||
basket.insert(String::from("banana"), 2);
|
basket.insert(String::from("banana"), 2);
|
||||||
|
|
||||||
// TODO: Put more fruits in your basket here.
|
// TODO: Put more fruits in your basket here.
|
||||||
|
basket.insert(String::from("apple"), 3);
|
||||||
|
basket.insert(String::from("pear"), 5);
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,8 +14,6 @@
|
||||||
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Hash, PartialEq, Eq)]
|
#[derive(Hash, PartialEq, Eq)]
|
||||||
|
@ -40,6 +38,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
||||||
// TODO: Insert new fruits if they are not already present in the
|
// 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
|
// basket. Note that you are not allowed to put any type of fruit that's
|
||||||
// already present!
|
// already present!
|
||||||
|
basket.entry(fruit).or_insert(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,6 @@
|
||||||
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// A structure to store the goal details of a team.
|
// A structure to store the goal details of a team.
|
||||||
|
@ -30,6 +28,7 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||||
|
|
||||||
for r in results.lines() {
|
for r in results.lines() {
|
||||||
let v: Vec<&str> = r.split(',').collect();
|
let v: Vec<&str> = r.split(',').collect();
|
||||||
|
println!("result { }", r);
|
||||||
let team_1_name = v[0].to_string();
|
let team_1_name = v[0].to_string();
|
||||||
let team_1_score: u8 = v[2].parse().unwrap();
|
let team_1_score: u8 = v[2].parse().unwrap();
|
||||||
let team_2_name = v[1].to_string();
|
let team_2_name = v[1].to_string();
|
||||||
|
@ -39,7 +38,23 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
||||||
// will be the number of goals conceded by team_2, and similarly
|
// will be the number of goals conceded by team_2, and similarly
|
||||||
// goals scored by team_2 will be the number of goals conceded by
|
// goals scored by team_2 will be the number of goals conceded by
|
||||||
// team_1.
|
// team_1.
|
||||||
}
|
let team_1 = scores.entry(team_1_name).or_insert(Team {
|
||||||
|
goals_scored: 0,
|
||||||
|
goals_conceded: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
team_1.goals_scored += team_1_score;
|
||||||
|
team_1.goals_conceded += team_2_score;
|
||||||
|
|
||||||
|
let team_2 = scores.entry(team_2_name).or_insert(Team {
|
||||||
|
goals_scored: 0,
|
||||||
|
goals_conceded: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
team_2.goals_scored += team_2_score;
|
||||||
|
team_2.goals_conceded += team_1_score;
|
||||||
|
};
|
||||||
|
|
||||||
scores
|
scores
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue