hashmaps done but didn't understand hashmap3.rs🦀

This commit is contained in:
Himan-Miku 2023-04-20 23:02:19 +05:30
parent dc50a86647
commit 6593e7ed86
4 changed files with 26 additions and 7 deletions

View File

@ -10,15 +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);
basket.insert(String::from("apple"), 0);
basket.insert(String::from("mango"), 3);
// TODO: Put more fruits in your basket here.

View File

@ -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)]
@ -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.
// Note that you are not allowed to put any type of fruit that's already
// present!
basket.entry(fruit).or_insert(1);
}
}

View File

@ -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.
@ -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
// goals scored by team_2 will be the number of goals conceded by
// 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
}

BIN
temp_15636_ThreadId1.exe Normal file

Binary file not shown.