mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-12 13:49:19 +00:00
finished hashmaps
This commit is contained in:
parent
b8452500d0
commit
244178b9a6
@ -10,17 +10,18 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint hashmaps1` or use the `hint` watch subcommand for a 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("pineapple"), 3);
|
||||||
|
|
||||||
basket
|
basket
|
||||||
}
|
}
|
||||||
|
|||||||
@ -11,8 +11,6 @@
|
|||||||
//
|
//
|
||||||
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Hash, PartialEq, Eq)]
|
#[derive(Hash, PartialEq, Eq)]
|
||||||
@ -37,6 +35,10 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
|||||||
// TODO: Put new fruits if not already present. Note that you
|
// TODO: Put new fruits if not already present. Note that you
|
||||||
// are not allowed to put any type of fruit that's already
|
// are not allowed to put any type of fruit that's already
|
||||||
// present!
|
// present!
|
||||||
|
|
||||||
|
if !basket.contains_key(&fruit) {
|
||||||
|
basket.insert(fruit, 2);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -14,8 +14,6 @@
|
|||||||
|
|
||||||
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
// A structure to store team name and its goal details.
|
// A structure to store team name and its goal details.
|
||||||
@ -40,6 +38,36 @@ fn build_scores_table(results: String) -> HashMap<String, Team> {
|
|||||||
// will be the number of goals conceded from team_2, and similarly
|
// 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
|
// goals scored by team_2 will be the number of goals conceded by
|
||||||
// team_1.
|
// team_1.
|
||||||
|
|
||||||
|
if (scores.contains_key(&team_1_name)) {
|
||||||
|
let team = scores.get_mut(&team_1_name).unwrap();
|
||||||
|
team.goals_scored += team_1_score;
|
||||||
|
team.goals_conceded += team_2_score;
|
||||||
|
} else {
|
||||||
|
scores.insert(
|
||||||
|
team_1_name.clone(),
|
||||||
|
Team {
|
||||||
|
name: team_1_name.clone(),
|
||||||
|
goals_scored: team_1_score,
|
||||||
|
goals_conceded: team_2_score,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (scores.contains_key(&team_2_name)) {
|
||||||
|
let team = scores.get_mut(&team_2_name).unwrap();
|
||||||
|
team.goals_scored += team_2_score;
|
||||||
|
team.goals_conceded += team_1_score;
|
||||||
|
} else {
|
||||||
|
scores.insert(
|
||||||
|
team_2_name.clone(),
|
||||||
|
Team {
|
||||||
|
name: team_2_name.clone(),
|
||||||
|
goals_scored: team_2_score,
|
||||||
|
goals_conceded: team_1_score,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
scores
|
scores
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user