mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
hashmaps done but didn't understand hashmap3.rs🦀
This commit is contained in:
parent
dc50a86647
commit
6593e7ed86
@ -10,15 +10,15 @@
|
|||||||
//
|
//
|
||||||
// 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();
|
||||||
|
|
||||||
// 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);
|
||||||
|
basket.insert(String::from("apple"), 0);
|
||||||
|
basket.insert(String::from("mango"), 3);
|
||||||
|
|
||||||
// TODO: Put more fruits in your basket here.
|
// TODO: Put more fruits in your basket here.
|
||||||
|
|
||||||
|
|||||||
@ -13,8 +13,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)]
|
||||||
@ -39,6 +37,7 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
|
|||||||
// TODO: Insert new fruits if they are not already present in the basket.
|
// 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
|
// Note that you are not allowed to put any type of fruit that's already
|
||||||
// present!
|
// present!
|
||||||
|
basket.entry(fruit).or_insert(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -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,28 @@ 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.
|
||||||
|
scores
|
||||||
|
.entry(team_1_name)
|
||||||
|
.and_modify(|t| {
|
||||||
|
t.goals_scored += team_1_score;
|
||||||
|
t.goals_conceded += team_2_score;
|
||||||
|
})
|
||||||
|
.or_insert_with_key(|team_name| Team {
|
||||||
|
name: team_name.to_string(),
|
||||||
|
goals_scored: team_1_score,
|
||||||
|
goals_conceded: team_2_score,
|
||||||
|
});
|
||||||
|
scores
|
||||||
|
.entry(team_2_name)
|
||||||
|
.and_modify(|t| {
|
||||||
|
t.goals_scored += team_2_score;
|
||||||
|
t.goals_conceded += team_1_score;
|
||||||
|
})
|
||||||
|
.or_insert_with_key(|team_name| Team {
|
||||||
|
name: team_name.to_string(),
|
||||||
|
goals_scored: team_2_score,
|
||||||
|
goals_conceded: team_1_score,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
scores
|
scores
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
temp_15636_ThreadId1.exe
Normal file
BIN
temp_15636_ThreadId1.exe
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user