From 4fbc61111a6213ba8ef4481041942322a8b4f9c5 Mon Sep 17 00:00:00 2001 From: ACYee <3058722734@qq.com> Date: Wed, 16 Oct 2024 14:19:06 +0800 Subject: [PATCH] . --- exercises/hashmaps/hashmaps1.rs | 7 ++++--- exercises/hashmaps/hashmaps2.rs | 16 ++++++++++++++-- exercises/hashmaps/hashmaps3.rs | 18 +++++++++++++++--- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/exercises/hashmaps/hashmaps1.rs b/exercises/hashmaps/hashmaps1.rs index 80829eaa6..3eae0a676 100644 --- a/exercises/hashmaps/hashmaps1.rs +++ b/exercises/hashmaps/hashmaps1.rs @@ -11,18 +11,19 @@ // 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 { - let mut basket = // TODO: declare your hash map here. + let mut basket: HashMap = Default::default();// TODO: declare your hash map here. // 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"), 2); + basket.insert(String::from("pear"), 2); + basket } diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index a59256909..61e2c3341 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -14,8 +14,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)] @@ -40,6 +38,20 @@ fn fruit_basket(basket: &mut HashMap) { // 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! + + match fruit { + Fruit::Banana => { + basket.insert(Fruit::Banana, 2); + }, + + Fruit::Pineapple => { + basket.insert(Fruit::Pineapple, 2); + }, + + _ => { + + } + } } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 08e977c33..9edd98677 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -3,7 +3,7 @@ // A list of scores (one per line) of a soccer match is given. Each line is of // the form : ",,," // Example: England,France,4,2 (England scored 4 goals, France 2). -// + // You have to build a scores table containing the name of the team, goals the // team scored, and goals the team conceded. One approach to build the scores // table is to use a Hashmap. The solution is partially written to use a @@ -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 the goal details of a team. @@ -39,6 +37,20 @@ fn build_scores_table(results: String) -> HashMap { // 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. + + let team1 = scores.entry(team_1_name).or_insert(Team { + goals_scored: 0, + goals_conceded: 0 + }); + team1.goals_scored += team_1_score; + team1.goals_conceded += team_2_score; + + let team2 = scores.entry(team_2_name).or_insert(Team { + goals_scored: 0, + goals_conceded: 0 + }); + team2.goals_scored += team_2_score; + team2.goals_conceded += team_1_score; } scores }