From dc6de2e9b3355fe9d84b2db7eb7bba735ee5518f Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Wed, 18 May 2022 21:33:45 -0400 Subject: [PATCH 1/2] quiz2 done --- exercises/collections/hashmap1.rs | 2 -- exercises/collections/hashmap2.rs | 9 +++++++-- exercises/error_handling/errors1.rs | 9 +++------ exercises/quiz2.rs | 22 ++++++++++------------ exercises/strings/strings1.rs | 6 ++---- exercises/strings/strings2.rs | 4 +--- 6 files changed, 23 insertions(+), 29 deletions(-) diff --git a/exercises/collections/hashmap1.rs b/exercises/collections/hashmap1.rs index 52e4ec42..724b0c7a 100644 --- a/exercises/collections/hashmap1.rs +++ b/exercises/collections/hashmap1.rs @@ -11,8 +11,6 @@ // Execute the command `rustlings hint hashmap1` if you need // hints. -// I AM NOT DONE - use std::collections::HashMap; fn fruit_basket() -> HashMap { diff --git a/exercises/collections/hashmap2.rs b/exercises/collections/hashmap2.rs index 0abe19ab..cd3aa4b1 100644 --- a/exercises/collections/hashmap2.rs +++ b/exercises/collections/hashmap2.rs @@ -12,8 +12,6 @@ // Execute the command `rustlings hint hashmap2` if you need // hints. -// I AM NOT DONE - use std::collections::HashMap; #[derive(Hash, PartialEq, Eq)] @@ -38,6 +36,13 @@ fn fruit_basket(basket: &mut HashMap) { // TODO: Put new fruits if not already present. Note that you // are not allowed to put any type of fruit that's already // present! + match fruit { + Fruit::Apple => basket.entry(fruit).or_insert(4), + Fruit::Mango => basket.entry(fruit).or_insert(2), + Fruit::Lychee => basket.entry(fruit).or_insert(5), + Fruit::Pineapple => basket.entry(fruit).or_insert(5), + Fruit::Banana => basket.entry(fruit).or_insert(5), + }; } } diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs index c417fb26..dd219f0f 100644 --- a/exercises/error_handling/errors1.rs +++ b/exercises/error_handling/errors1.rs @@ -5,14 +5,11 @@ // construct to `Option` that can be used to express error conditions. Let's use it! // Execute `rustlings hint errors1` for hints! -// I AM NOT DONE - -pub fn generate_nametag_text(name: String) -> Option { +pub fn generate_nametag_text(name: String) -> Result { if name.len() > 0 { - Some(format!("Hi! My name is {}", name)) + Ok(format!("Hi! My name is {}", name)) } else { - // Empty names aren't allowed. - None + Err("`name` was empty; it must be nonempty.".to_string()) } } diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index de0dce95..37c33a53 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -7,8 +7,6 @@ // you think each value is. That is, add either `string_slice` or `string` // before the parentheses on each line. If you're right, it will compile! -// I AM NOT DONE - fn string_slice(arg: &str) { println!("{}", arg); } @@ -17,14 +15,14 @@ fn string(arg: String) { } fn main() { - ???("blue"); - ???("red".to_string()); - ???(String::from("hi")); - ???("rust is fun!".to_owned()); - ???("nice weather".into()); - ???(format!("Interpolation {}", "Station")); - ???(&String::from("abc")[0..1]); - ???(" hello there ".trim()); - ???("Happy Monday!".to_string().replace("Mon", "Tues")); - ???("mY sHiFt KeY iS sTiCkY".to_lowercase()); + string_slice("blue"); + string("red".to_string()); + string(String::from("hi")); + string("rust is fun!".to_owned()); + string("nice weather".into()); + string(format!("Interpolation {}", "Station")); + string_slice(&String::from("abc")[0..1]); + string_slice(" hello there ".trim()); + string("Happy Monday!".to_string().replace("Mon", "Tues")); + string("mY sHiFt KeY iS sTiCkY".to_lowercase()); } diff --git a/exercises/strings/strings1.rs b/exercises/strings/strings1.rs index 80902444..748380ce 100644 --- a/exercises/strings/strings1.rs +++ b/exercises/strings/strings1.rs @@ -1,8 +1,6 @@ // strings1.rs // Make me compile without changing the function signature! -// Execute `rustlings hint strings1` for hints ;) - -// I AM NOT DONE +// Execute `rustlings hint strings1` for hints ; fn main() { let answer = current_favorite_color(); @@ -10,5 +8,5 @@ fn main() { } fn current_favorite_color() -> String { - "blue" + "blue".to_string() } diff --git a/exercises/strings/strings2.rs b/exercises/strings/strings2.rs index 5a2ce74a..a048f882 100644 --- a/exercises/strings/strings2.rs +++ b/exercises/strings/strings2.rs @@ -2,11 +2,9 @@ // Make me compile without changing the function signature! // Execute `rustlings hint strings2` for hints :) -// I AM NOT DONE - fn main() { let word = String::from("green"); // Try not changing this line :) - if is_a_color_word(word) { + if is_a_color_word(&word) { println!("That is a color word I know!"); } else { println!("That is not a color word I know."); From 1d11dab373b703a32eecd218593223a360704995 Mon Sep 17 00:00:00 2001 From: Chris Girvin Date: Wed, 18 May 2022 23:28:59 -0400 Subject: [PATCH 2/2] errors4 --- exercises/error_handling/errors2.rs | 4 +--- exercises/error_handling/errors3.rs | 4 +--- exercises/error_handling/errors4.rs | 10 +++++++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs index aad3a93f..478e3e45 100644 --- a/exercises/error_handling/errors2.rs +++ b/exercises/error_handling/errors2.rs @@ -16,14 +16,12 @@ // There are at least two ways to implement this that are both correct-- but // one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways. -// I AM NOT DONE - use std::num::ParseIntError; pub fn total_cost(item_quantity: &str) -> Result { let processing_fee = 1; let cost_per_item = 5; - let qty = item_quantity.parse::(); + let qty = item_quantity.parse::()?; Ok(qty * cost_per_item + processing_fee) } diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs index 460ac5c4..b799ad8b 100644 --- a/exercises/error_handling/errors3.rs +++ b/exercises/error_handling/errors3.rs @@ -4,15 +4,13 @@ // Why not? What should we do to fix it? // Execute `rustlings hint errors3` for hints! -// I AM NOT DONE - use std::num::ParseIntError; fn main() { let mut tokens = 100; let pretend_user_input = "8"; - let cost = total_cost(pretend_user_input)?; + let cost = total_cost(pretend_user_input).unwrap(); if cost > tokens { println!("You can't afford that many!"); diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs index 0685c374..9bec80ab 100644 --- a/exercises/error_handling/errors4.rs +++ b/exercises/error_handling/errors4.rs @@ -1,8 +1,6 @@ // errors4.rs // Make this test pass! Execute `rustlings hint errors4` for hints :) -// I AM NOT DONE - #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -14,7 +12,13 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { - Ok(PositiveNonzeroInteger(value as u64)) + if value >= 1 { + Ok(PositiveNonzeroInteger(value as u64)) + } else if value == 0 { + Err(CreationError::Zero) + } else { + Err(CreationError::Negative) + } } }