From 40bb999653ead91cae9beb0db9289e653ef5aa72 Mon Sep 17 00:00:00 2001 From: saiyuan233 <1765930676@qq.com> Date: Tue, 24 Dec 2024 21:56:12 +0800 Subject: [PATCH] update --- exercises/hashmaps/hashmaps2.rs | 3 ++- exercises/hashmaps/hashmaps3.rs | 15 ++++++++++++++- exercises/quiz2.rs | 18 +++++++++++++++--- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/exercises/hashmaps/hashmaps2.rs b/exercises/hashmaps/hashmaps2.rs index a59256909..bc0537c82 100644 --- a/exercises/hashmaps/hashmaps2.rs +++ b/exercises/hashmaps/hashmaps2.rs @@ -14,7 +14,7 @@ // Execute `rustlings hint hashmaps2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + use std::collections::HashMap; @@ -40,6 +40,7 @@ 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! + basket.entry(fruit).or_insert(10); } } diff --git a/exercises/hashmaps/hashmaps3.rs b/exercises/hashmaps/hashmaps3.rs index 08e977c33..9f09dbf4a 100644 --- a/exercises/hashmaps/hashmaps3.rs +++ b/exercises/hashmaps/hashmaps3.rs @@ -14,7 +14,7 @@ // Execute `rustlings hint hashmaps3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE + use std::collections::HashMap; @@ -39,6 +39,19 @@ 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 score = scores.entry(team_1_name.clone()).or_insert(Team{ + goals_scored: 0, + goals_conceded: 0 + }); + (*score).goals_scored += team_1_score; + (*score).goals_conceded += team_2_score; + + let score = scores.entry(team_2_name.clone()).or_insert(Team{ + goals_scored: 0, + goals_conceded: 0 + }); + (*score).goals_scored += team_2_score; + (*score).goals_conceded += team_1_score; } scores } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 29925cafc..19146e864 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -32,11 +32,23 @@ mod my_module { use super::Command; // TODO: Complete the function signature! - pub fn transformer(input: ???) -> ??? { + pub fn transformer(input: Vec<(String, Command)>) -> Vec { // TODO: Complete the output declaration! - let mut output: ??? = vec![]; + let mut output: Vec = vec![]; for (string, command) in input.iter() { // TODO: Complete the function body. You can do it! + let transformer_string = match command { + Command::Uppercase => string.to_uppercase(), + Command::Trim => string.trim().to_string(), + Command::Append(n) => { + let mut s = String::from(string); + for _ in 0..*n { + s.push_str("bar"); + } + s.to_string() + } + }; + output.push(transformer_string); } output } @@ -45,7 +57,7 @@ mod my_module { #[cfg(test)] mod tests { // TODO: What do we need to import to have `transformer` in scope? - use ???; + use super::my_module::transformer; use super::Command; #[test]