summaryrefslogtreecommitdiff
path: root/rustlings/exercises
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/exercises')
-rw-r--r--rustlings/exercises/11_hashmaps/hashmaps1.rs5
-rw-r--r--rustlings/exercises/11_hashmaps/hashmaps2.rs8
-rw-r--r--rustlings/exercises/11_hashmaps/hashmaps3.rs7
-rw-r--r--rustlings/exercises/quizzes/quiz2.rs18
4 files changed, 34 insertions, 4 deletions
diff --git a/rustlings/exercises/11_hashmaps/hashmaps1.rs b/rustlings/exercises/11_hashmaps/hashmaps1.rs
index 74001d0..a746b32 100644
--- a/rustlings/exercises/11_hashmaps/hashmaps1.rs
+++ b/rustlings/exercises/11_hashmaps/hashmaps1.rs
@@ -8,12 +8,15 @@ use std::collections::HashMap;
fn fruit_basket() -> HashMap<String, u32> {
// TODO: Declare the hash map.
- // let mut basket =
+ let mut basket = HashMap::new();
// Two bananas are already given for you :)
basket.insert(String::from("banana"), 2);
// TODO: Put more fruits in your basket.
+ basket.insert(String::from("banana"), 1);
+ basket.insert(String::from("orange"), 140000);
+ basket.insert(String::from("strawberry"), 5);
basket
}
diff --git a/rustlings/exercises/11_hashmaps/hashmaps2.rs b/rustlings/exercises/11_hashmaps/hashmaps2.rs
index e9f53fe..482db7c 100644
--- a/rustlings/exercises/11_hashmaps/hashmaps2.rs
+++ b/rustlings/exercises/11_hashmaps/hashmaps2.rs
@@ -29,9 +29,11 @@ fn fruit_basket(basket: &mut HashMap<Fruit, u32>) {
];
for fruit in fruit_kinds {
- // 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!
+ // This works just fine, but clippy doesn't like how readable and obvious the code is
+ // if !basket.contains_key(&fruit) {
+ // basket.insert(fruit, 10);
+ // }
+ basket.entry(fruit).or_insert(10);
}
}
diff --git a/rustlings/exercises/11_hashmaps/hashmaps3.rs b/rustlings/exercises/11_hashmaps/hashmaps3.rs
index 5b390ab..0fa4a6b 100644
--- a/rustlings/exercises/11_hashmaps/hashmaps3.rs
+++ b/rustlings/exercises/11_hashmaps/hashmaps3.rs
@@ -31,6 +31,13 @@ fn build_scores_table(results: &str) -> HashMap<&str, TeamScores> {
// Keep in mind that goals scored by team 1 will be the number of goals
// conceded by team 2. Similarly, goals scored by team 2 will be the
// number of goals conceded by team 1.
+ let team_1 = scores.entry(team_1_name).or_default();
+ team_1.goals_scored += team_1_score;
+ team_1.goals_conceded += team_2_score;
+ let team_2 = scores.entry(team_2_name).or_default();
+ team_2.goals_scored += team_2_score;
+ team_2.goals_conceded += team_1_score;
+
}
scores
diff --git a/rustlings/exercises/quizzes/quiz2.rs b/rustlings/exercises/quizzes/quiz2.rs
index 2cddba9..fd32810 100644
--- a/rustlings/exercises/quizzes/quiz2.rs
+++ b/rustlings/exercises/quizzes/quiz2.rs
@@ -28,6 +28,23 @@ mod my_module {
// TODO: Complete the function as described above.
// pub fn transformer(input: ???) -> ??? { ??? }
+ pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
+ let mut out = Vec::new();
+ for inp in input {
+ out.push(match inp.1 {
+ Command::Uppercase => inp.0.to_uppercase(),
+ Command::Trim => String::from(inp.0.trim()),
+ Command::Append(n) => {
+ let mut r = inp.0;
+ for _ in 0..n {
+ r += "bar";
+ }
+ r
+ }
+ });
+ }
+ out
+ }
}
fn main() {
@@ -39,6 +56,7 @@ mod tests {
// TODO: What do we need to import to have `transformer` in scope?
// use ???;
use super::Command;
+ use super::my_module::transformer;
#[test]
fn it_works() {