diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs index 9c24d85d..19241547 100644 --- a/exercises/error_handling/errors1.rs +++ b/exercises/error_handling/errors1.rs @@ -6,14 +6,10 @@ // this function to have. // Execute `rustlings hint errors1` for hints! -// I AM NOT DONE - -pub fn generate_nametag_text(name: String) -> Option { - if name.len() > 0 { - Some(format!("Hi! My name is {}", name)) - } else { - // Empty names aren't allowed. - None +pub fn generate_nametag_text(name: String) -> Result { + match name.len() > 0 { + true => Ok(format!("Hi! My name is {}", name)), + _ => Err("`name` was empty; it must be nonempty.".into()), } } @@ -28,7 +24,7 @@ mod tests { fn generates_nametag_text_for_a_nonempty_name() { assert_eq!( generate_nametag_text("Beyoncé".into()), - Some("Hi! My name is Beyoncé".into()) + Ok("Hi! My name is Beyoncé".into()) ); } 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..5d3067fb 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,6 +12,11 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { + if value == 0 { + return Err(CreationError::Zero); + } else 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 365a8691..66889a33 100644 --- a/exercises/error_handling/errors5.rs +++ b/exercises/error_handling/errors5.rs @@ -4,14 +4,12 @@ // It won't compile right now! Why? // Execute `rustlings hint errors5` for hints! -// 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<(), ParseIntError> { +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 0f6b27a6..70b470c8 100644 --- a/exercises/error_handling/errors6.rs +++ b/exercises/error_handling/errors6.rs @@ -8,8 +8,6 @@ // Make these tests pass! Execute `rustlings hint errors6` for hints :) -// I AM NOT DONE - use std::num::ParseIntError; // This is a custom error type that we will be using in `parse_pos_nonzero()`. @@ -24,14 +22,16 @@ impl ParsePosNonzeroError { ParsePosNonzeroError::Creation(err) } // TODO: add another error conversion function here. + fn from_parse_int(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 = s.parse() + .map_err(ParsePosNonzeroError::from_parse_int)?; PositiveNonzeroInteger::new(x) .map_err(ParsePosNonzeroError::from_creation) }