diff --git a/exercises/13_error_handling/errors1.rs b/exercises/13_error_handling/errors1.rs index 0ba59a57..56d0e11e 100644 --- a/exercises/13_error_handling/errors1.rs +++ b/exercises/13_error_handling/errors1.rs @@ -9,14 +9,13 @@ // 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.".to_string()) } else { - Some(format!("Hi! My name is {}", name)) + Ok(format!("Hi! My name is {}", name)) } } diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs index 631fe67f..58d0958d 100644 --- a/exercises/13_error_handling/errors2.rs +++ b/exercises/13_error_handling/errors2.rs @@ -19,16 +19,22 @@ // 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::(); + // Same as the match expression below + let qty = item_quantity.parse::()?; + + // match item_quantity.parse::() { + // Ok(quantity) => Ok(quantity * cost_per_item + processing_fee), + // Err(e) => Err(e), + // } - Ok(qty * cost_per_item + processing_fee) + // I tried to use the if let statement but that's for Pattern matching + // This is not patter matching because it's looking for conditional checks. } #[cfg(test)] diff --git a/exercises/13_error_handling/errors3.rs b/exercises/13_error_handling/errors3.rs index d42d3b17..b8156f51 100644 --- a/exercises/13_error_handling/errors3.rs +++ b/exercises/13_error_handling/errors3.rs @@ -7,11 +7,17 @@ // Execute `rustlings hint errors3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::num::ParseIntError; +use std::error; -fn main() { +// I could have just returned the ParseIntError from the total_cost function +// because we know the exact error that's being returned. + +fn main() -> Result<(), Box>{ + // Box is a trait object. + // Meaning, you would use this when you know an error can be returned but + // not necessarily what kind + let mut tokens = 100; let pretend_user_input = "8"; @@ -23,6 +29,8 @@ fn main() { tokens -= cost; println!("You now have {} tokens.", tokens); } + + Ok(()) } pub fn total_cost(item_quantity: &str) -> Result { diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index d6d6fcb6..746af9ae 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint errors4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -17,7 +16,13 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { // Hmm... Why is this always returning an Ok value? - Ok(PositiveNonzeroInteger(value as u64)) + if value > 0 { + Ok(PositiveNonzeroInteger(value as u64)) + } else if value == 0 { + Err(CreationError::Zero) + } else { + Err(CreationError::Negative) + } } } diff --git a/exercises/13_error_handling/errors5.rs b/exercises/13_error_handling/errors5.rs index 92461a7e..797a60d4 100644 --- a/exercises/13_error_handling/errors5.rs +++ b/exercises/13_error_handling/errors5.rs @@ -22,14 +22,12 @@ // Execute `rustlings hint errors5` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - use std::error; use std::fmt; 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/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs index aaf0948e..4d8f8aa8 100644 --- a/exercises/13_error_handling/errors6.rs +++ b/exercises/13_error_handling/errors6.rs @@ -9,7 +9,6 @@ // Execute `rustlings hint errors6` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE use std::num::ParseIntError; @@ -26,12 +25,15 @@ impl ParsePosNonzeroError { } // 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) }