diff --git a/exercises/error_handling/README.md b/exercises/error_handling/README.md index 5255ace9..a87a45f4 100644 --- a/exercises/error_handling/README.md +++ b/exercises/error_handling/README.md @@ -1,4 +1,5 @@ # Error handling + Most errors aren’t serious enough to require the program to stop entirely. Sometimes, when a function fails, it’s for a reason that you can easily interpret and respond to. For example, if you try to open a file and that operation fails because the file doesn’t exist, you might want to create the file instead of terminating the process. diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs index bcee9723..6bfe94a0 100644 --- a/exercises/error_handling/errors1.rs +++ b/exercises/error_handling/errors1.rs @@ -5,14 +5,12 @@ // construct to `Option` that can be used to express error conditions. Let's use it! // Execute `rustlings hint errors1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - -pub fn generate_nametag_text(name: String) -> Option { +pub fn generate_nametag_text(name: String) -> Result { if name.is_empty() { // Empty names aren't allowed. - None + Err("`name` was empty; it must be nonempty.".into()) } else { - Some(format!("Hi! My name is {}", name)) + Ok(format!("Hi! My name is {}", name)) } } diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs index 1cd8fc66..0bd0530a 100644 --- a/exercises/error_handling/errors2.rs +++ b/exercises/error_handling/errors2.rs @@ -17,14 +17,12 @@ // one is a lot shorter! // Execute `rustlings hint errors2` or use the `hint` watch subcommand for a hint. -// 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 a2d2d190..e0ebc9b9 100644 --- a/exercises/error_handling/errors3.rs +++ b/exercises/error_handling/errors3.rs @@ -4,11 +4,10 @@ // Why not? What should we do to fix it? // Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - +use std::error::Error; use std::num::ParseIntError; -fn main() { +fn main() -> Result<(), Box> { let mut tokens = 100; let pretend_user_input = "8"; @@ -20,6 +19,8 @@ fn main() { tokens -= cost; println!("You now have {} tokens.", tokens); } + + Ok(()) } pub fn total_cost(item_quantity: &str) -> Result { diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs index 0efe8ccd..63ae59ce 100644 --- a/exercises/error_handling/errors4.rs +++ b/exercises/error_handling/errors4.rs @@ -1,7 +1,7 @@ // errors4.rs // Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE +use std::f32::consts::E; #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -15,6 +15,14 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { // Hmm...? Why is this only returning an Ok value? + + if value == 0 { + return Err(CreationError::Zero); + } + if value < 0 { + return Err(CreationError::Negative); + } + Ok(PositiveNonzeroInteger(value as u64)) } } diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs index 6da06ef3..1505c0f7 100644 --- a/exercises/error_handling/errors5.rs +++ b/exercises/error_handling/errors5.rs @@ -16,14 +16,14 @@ // Execute `rustlings hint errors5` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::error; +use std::error::Error; use std::fmt; +use std::fmt::{Display, Formatter}; use std::num::ParseIntError; // TODO: update the return type of `main()` to make this compile. -fn main() -> Result<(), Box> { +fn main() -> Result<(), Box> { let pretend_user_input = "42"; let x: i64 = pretend_user_input.parse()?; println!("output={:?}", PositiveNonzeroInteger::new(x)?); diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs index 8097b490..8db20b94 100644 --- a/exercises/error_handling/errors6.rs +++ b/exercises/error_handling/errors6.rs @@ -8,8 +8,6 @@ // Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::num::ParseIntError; // This is a custom error type that we will be using in `parse_pos_nonzero()`. @@ -23,14 +21,17 @@ impl ParsePosNonzeroError { fn from_creation(err: CreationError) -> ParsePosNonzeroError { ParsePosNonzeroError::Creation(err) } + // TODO: add another error conversion function here. - // fn from_parseint... + fn from_parseint(err: ParseIntError) -> ParsePosNonzeroError { + ParsePosNonzeroError::ParseInt(err) + } } fn parse_pos_nonzero(s: &str) -> Result { // TODO: change this to return an appropriate error instead of panicking // when `parse()` returns an error. - let x: i64 = s.parse().unwrap(); + let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation) } diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index 1f891b0e..990394e6 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -1,8 +1,6 @@ // options1.rs // Execute `rustlings hint options1` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - // This function returns how much icecream there is left in the fridge. // If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them // all, so there'll be no more left :( @@ -10,7 +8,13 @@ fn maybe_icecream(time_of_day: u16) -> Option { // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a value of 0 // The Option output should gracefully handle cases where time_of_day > 23. // TODO: Complete the function body - remember to return an Option! - ??? + if time_of_day < 22 { + return Some(5); + } else if time_of_day < 24 { + return Some(0); + } + + return None; } #[cfg(test)] @@ -30,6 +34,6 @@ mod tests { fn raw_value() { // TODO: Fix this test. How do you get at the value contained in the Option? let icecreams = maybe_icecream(12); - assert_eq!(icecreams, 5); + assert_eq!(icecreams.unwrap(), 5); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index 4e36443f..61c1fa83 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -1,8 +1,6 @@ // options2.rs // Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[cfg(test)] mod tests { #[test] @@ -11,7 +9,7 @@ mod tests { let optional_target = Some(target); // TODO: Make this an if let statement whose value is "Some" type - word = optional_target { + if let Some(word) = optional_target { assert_eq!(word, target); } } @@ -26,8 +24,8 @@ mod tests { // TODO: make this a while let statement - remember that vector.pop also adds another layer of Option // You can stack `Option`'s into while let and if let - integer = optional_integers.pop() { - assert_eq!(integer, range); + while let Some(integer) = optional_integers.pop() { + assert_eq!(integer.unwrap(), range); range -= 1; } } diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs index 3f995c52..1dd0b374 100644 --- a/exercises/options/options3.rs +++ b/exercises/options/options3.rs @@ -1,8 +1,6 @@ // options3.rs // Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - struct Point { x: i32, y: i32, @@ -12,7 +10,7 @@ fn main() { let y: Option = Some(Point { x: 100, y: 200 }); match y { - Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), + Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => println!("no match"), } y; // Fix without deleting this line. diff --git a/exercises/quiz2.rs b/exercises/quiz2.rs index 5fa9ef5d..5b204273 100644 --- a/exercises/quiz2.rs +++ b/exercises/quiz2.rs @@ -28,7 +28,7 @@ mod my_module { use super::Command; // TODO: Complete the function signature! - pub fn transformer(input: Vec<(String, Command)>) -> Vec { + pub fn transformer(input: Vec<(&str, Command)>) -> Vec { // TODO: Complete the output declaration! let mut output = vec![]; for (string, command) in input.iter() { @@ -41,7 +41,7 @@ mod my_module { output.push(string.trim().to_string()); } Command::Uppercase => { - output.push(string.to_uppercase().to_string()); + output.push(string.to_uppercase()); } } }