From 50ab289da6b9eb19a7486c341b00048c516b88c0 Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Sun, 6 Jun 2021 20:14:29 -0500 Subject: [PATCH 1/3] fix: rename result1 to errors4 Also put it in the ERROR HANDLING section where it probably belongs. --- .../error_handling/{result1.rs => errors4.rs} | 4 ++-- info.toml | 20 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) rename exercises/error_handling/{result1.rs => errors4.rs} (88%) diff --git a/exercises/error_handling/result1.rs b/exercises/error_handling/errors4.rs similarity index 88% rename from exercises/error_handling/result1.rs rename to exercises/error_handling/errors4.rs index b978001..0685c37 100644 --- a/exercises/error_handling/result1.rs +++ b/exercises/error_handling/errors4.rs @@ -1,5 +1,5 @@ -// result1.rs -// Make this test pass! Execute `rustlings hint result1` for hints :) +// errors4.rs +// Make this test pass! Execute `rustlings hint errors4` for hints :) // I AM NOT DONE diff --git a/info.toml b/info.toml index 8c8f7c1..55875d7 100644 --- a/info.toml +++ b/info.toml @@ -489,6 +489,15 @@ mode = "compile" hint = """ If other functions can return a `Result`, why shouldn't `main`?""" +[[exercises]] +name = "errors4" +path = "exercises/error_handling/errors4.rs" +mode = "test" +hint = """ +`PositiveNonzeroInteger::new` is always creating a new instance and returning an `Ok` result. +It should be doing some checking, returning an `Err` result if those checks fail, and only +returning an `Ok` result if those checks determine that everything is... okay :)""" + [[exercises]] name = "errorsn" path = "exercises/error_handling/errorsn.rs" @@ -561,7 +570,7 @@ ReportCard struct generic, but also the correct property - you will need to chan of the struct slightly too...you can do it! """ -# OPTIONS / RESULTS +# OPTIONS [[exercises]] name = "option1" @@ -603,15 +612,6 @@ statement. How can this be avoided? The compiler shows the correction needed. After making the correction as suggested by the compiler, do read: https://doc.rust-lang.org/std/keyword.ref.html""" -[[exercises]] -name = "result1" -path = "exercises/error_handling/result1.rs" -mode = "test" -hint = """ -`PositiveNonzeroInteger::new` is always creating a new instance and returning an `Ok` result. -It should be doing some checking, returning an `Err` result if those checks fail, and only -returning an `Ok` result if those checks determine that everything is... okay :)""" - # TRAITS [[exercises]] From 68d3ac567cd5c23f5593c2f4df51612bca3d09a9 Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Sun, 6 Jun 2021 23:05:01 -0500 Subject: [PATCH 2/3] feature: improve error_handling exercises Add new exercises errors5 and errors6, to introduce boxed errors and custom error enums more gently. Delete errorsn, because it tried to do too much too soon. --- exercises/error_handling/errors5.rs | 53 +++++++++++++ exercises/error_handling/errors6.rs | 86 ++++++++++++++++++++ exercises/error_handling/errorsn.rs | 117 ---------------------------- info.toml | 61 ++++++++------- 4 files changed, 173 insertions(+), 144 deletions(-) create mode 100644 exercises/error_handling/errors5.rs create mode 100644 exercises/error_handling/errors6.rs delete mode 100644 exercises/error_handling/errorsn.rs diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs new file mode 100644 index 0000000..1b11800 --- /dev/null +++ b/exercises/error_handling/errors5.rs @@ -0,0 +1,53 @@ +// errors5.rs + +// This program uses a completed version of the code from errors4. +// 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> { + let pretend_user_input = "42"; + let x: i64 = pretend_user_input.parse()?; + println!("output={:?}", PositiveNonzeroInteger::new(x)?); + Ok(()) +} + +// Don't change anything below this line. + +#[derive(PartialEq, Debug)] +struct PositiveNonzeroInteger(u64); + +#[derive(PartialEq, Debug)] +enum CreationError { + Negative, + Zero, +} + +impl PositiveNonzeroInteger { + fn new(value: i64) -> Result { + match value { + x if x < 0 => Err(CreationError::Negative), + x if x == 0 => Err(CreationError::Zero), + x => Ok(PositiveNonzeroInteger(x as u64)) + } + } +} + +// This is required so that `CreationError` can implement `error::Error`. +impl fmt::Display for CreationError { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let description = match *self { + CreationError::Negative => "Number is negative", + CreationError::Zero => "Number is zero", + }; + f.write_str(description) + } +} + +impl error::Error for CreationError {} diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs new file mode 100644 index 0000000..cee7250 --- /dev/null +++ b/exercises/error_handling/errors6.rs @@ -0,0 +1,86 @@ +// errors6.rs + +// Using catch-all error types like `Box` isn't recommended +// for library code, where callers might want to make decisions based on the +// error content, instead of printing it out or propagating it further. Here, +// we define a custom error type to make it possible for callers to decide +// what to do next when our function returns an error. + +// Make these tests pass! Execute `rustlings hint errors6` for hints :) + +// I AM NOT DONE + +// This is a custom error type that we will be using in `parse_pos_nonzero()`. +#[derive(PartialEq, Debug)] +enum ParsePosNonzeroError { + CreationError, + ParseIntError +} + +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(); + PositiveNonzeroInteger::new(x) + .or(Err(ParsePosNonzeroError::CreationError)) +} + +// Don't change anything below this line. + +#[derive(PartialEq, Debug)] +struct PositiveNonzeroInteger(u64); + +#[derive(PartialEq, Debug)] +enum CreationError { + Negative, + Zero, +} + +impl PositiveNonzeroInteger { + fn new(value: i64) -> Result { + match value { + x if x < 0 => Err(CreationError::Negative), + x if x == 0 => Err(CreationError::Zero), + x => Ok(PositiveNonzeroInteger(x as u64)) + } + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_parse_error() { + assert_eq!( + parse_pos_nonzero("not a number"), + Err(ParsePosNonzeroError::ParseIntError) + ); + } + + #[test] + fn test_negative() { + assert_eq!( + parse_pos_nonzero("-555"), + Err(ParsePosNonzeroError::CreationError) + ); + } + + #[test] + fn test_zero() { + assert_eq!( + parse_pos_nonzero("0"), + Err(ParsePosNonzeroError::CreationError) + ); + } + + #[test] + fn test_positive() { + assert_eq!( + parse_pos_nonzero("42"), + Ok(PositiveNonzeroInteger(42)) + ); + } +} diff --git a/exercises/error_handling/errorsn.rs b/exercises/error_handling/errorsn.rs deleted file mode 100644 index 5fe212b..0000000 --- a/exercises/error_handling/errorsn.rs +++ /dev/null @@ -1,117 +0,0 @@ -// errorsn.rs -// This is a bigger error exercise than the previous ones! -// You can do it! :) -// -// Edit the `read_and_validate` function ONLY. Don't create any Errors -// that do not already exist. -// -// So many things could go wrong! -// -// - Reading from stdin could produce an io::Error -// - Parsing the input could produce a num::ParseIntError -// - Validating the input could produce a CreationError (defined below) -// -// How can we lump these errors into one general error? That is, what -// type goes where the question marks are, and how do we return -// that type from the body of read_and_validate? -// -// Execute `rustlings hint errorsn` for hints :) - -// I AM NOT DONE - -use std::error; -use std::fmt; -use std::io; - -// PositiveNonzeroInteger is a struct defined below the tests. -fn read_and_validate(b: &mut dyn io::BufRead) -> Result { - let mut line = String::new(); - b.read_line(&mut line); - let num: i64 = line.trim().parse(); - let answer = PositiveNonzeroInteger::new(num); - answer -} - -// -// Nothing below this needs to be modified -// - -// This is a test helper function that turns a &str into a BufReader. -fn test_with_str(s: &str) -> Result> { - let mut b = io::BufReader::new(s.as_bytes()); - read_and_validate(&mut b) -} - -#[test] -fn test_success() { - let x = test_with_str("42\n"); - assert_eq!(PositiveNonzeroInteger(42), x.unwrap()); -} - -#[test] -fn test_not_num() { - let x = test_with_str("eleven billion\n"); - assert!(x.is_err()); -} - -#[test] -fn test_non_positive() { - let x = test_with_str("-40\n"); - assert!(x.is_err()); -} - -#[test] -fn test_ioerror() { - struct Broken; - impl io::Read for Broken { - fn read(&mut self, _buf: &mut [u8]) -> io::Result { - Err(io::Error::new(io::ErrorKind::BrokenPipe, "uh-oh!")) - } - } - let mut b = io::BufReader::new(Broken); - assert!(read_and_validate(&mut b).is_err()); - assert_eq!("uh-oh!", read_and_validate(&mut b).unwrap_err().to_string()); -} - -#[derive(PartialEq, Debug)] -struct PositiveNonzeroInteger(u64); - -impl PositiveNonzeroInteger { - fn new(value: i64) -> Result { - if value == 0 { - Err(CreationError::Zero) - } else if value < 0 { - Err(CreationError::Negative) - } else { - Ok(PositiveNonzeroInteger(value as u64)) - } - } -} - -#[test] -fn test_positive_nonzero_integer_creation() { - assert!(PositiveNonzeroInteger::new(10).is_ok()); - assert_eq!( - Err(CreationError::Negative), - PositiveNonzeroInteger::new(-10) - ); - assert_eq!(Err(CreationError::Zero), PositiveNonzeroInteger::new(0)); -} - -#[derive(PartialEq, Debug)] -enum CreationError { - Negative, - Zero, -} - -impl fmt::Display for CreationError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let description = match *self { - CreationError::Negative => "Number is negative", - CreationError::Zero => "Number is zero", - }; - f.write_str(description) - } -} - -impl error::Error for CreationError {} diff --git a/info.toml b/info.toml index 55875d7..63eb78b 100644 --- a/info.toml +++ b/info.toml @@ -499,42 +499,49 @@ It should be doing some checking, returning an `Err` result if those checks fail returning an `Ok` result if those checks determine that everything is... okay :)""" [[exercises]] -name = "errorsn" -path = "exercises/error_handling/errorsn.rs" -mode = "test" +name = "errors5" +path = "exercises/error_handling/errors5.rs" +mode = "compile" hint = """ -First hint: To figure out what type should go where the ??? is, take a look -at the test helper function `test_with_str`, since it returns whatever -`read_and_validate` returns and `test_with_str` has its signature fully -specified. - - -Next hint: There are three places in `read_and_validate` that we call a -function that returns a `Result` (that is, the functions might fail). -Apply the `?` operator on those calls so that we return immediately from -`read_and_validate` if those function calls fail. - +Hint: There are two different possible `Result` types produced within +`main()`, which are propagated using `?` operators. How do we declare a +return type from `main()` that allows both? Another hint: under the hood, the `?` operator calls `From::from` -on the error value to convert it to a boxed trait object, a Box, -which is polymorphic-- that means that lots of different kinds of errors -can be returned from the same function because all errors act the same -since they all implement the `error::Error` trait. +on the error value to convert it to a boxed trait object, a +`Box`, which is polymorphic-- that means that lots of +different kinds of errors can be returned from the same function because +all errors act the same since they all implement the `error::Error` trait. Check out this section of the book: https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator +This exercise uses some concepts that we won't get to until later in the +course, like `Box` and the `From` trait. It's not important to understand +them in detail right now, but you can read ahead if you like. -Another another hint: Note that because the `?` operator returns -the *unwrapped* value in the `Ok` case, if we want to return a `Result` from -`read_and_validate` for *its* success case, we'll have to rewrap a value -that we got from the return value of a `?`ed call in an `Ok`-- this will -look like `Ok(something)`. +Read more about boxing errors: +https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/boxing_errors.html +Read more about using the `?` operator with boxed errors: +https://doc.rust-lang.org/stable/rust-by-example/error/multiple_error_types/reenter_question_mark.html +""" -Another another another hint: `Result`s must be "used", that is, you'll -get a warning if you don't handle a `Result` that you get in your -function. Read more about that in the `std::result` module docs: -https://doc.rust-lang.org/std/result/#results-must-be-used""" +[[exercises]] +name = "errors6" +path = "exercises/error_handling/errors6.rs" +mode = "test" +hint = """ +This exercise uses a completed version of `PositiveNonzeroInteger` from +the errors4. + +Below the TODO line, there is an example of using the `.or()` method +on a `Result` to transform one type of error into another. Try using +something similar on the `Result` from `parse()`. You might use the `?` +operator to return early from the function, or you might use a `match` +expression, or maybe there's another way! + +Read more about `.or()` in the `std::result` documentation: +https://doc.rust-lang.org/std/result/enum.Result.html#method.or""" # Generics From b7ddd09fab97fc96f032bc8c0b9e1a64e5ffbcdd Mon Sep 17 00:00:00 2001 From: Taylor Yu Date: Wed, 9 Jun 2021 18:13:57 -0500 Subject: [PATCH 3/3] address review feedback Adjust error text and naming to conform with best practices. Use `map_err()` instead of `or()`. Wrap lower-level errors instead of ignoring their details. Also, don't "cheat" by bypassing the `new()` function in tests. Fix a dangling reference in the try_from_into hints. --- exercises/error_handling/errors5.rs | 4 ++-- exercises/error_handling/errors6.rs | 33 ++++++++++++++++++----------- info.toml | 21 ++++++++++-------- 3 files changed, 35 insertions(+), 23 deletions(-) diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs index 1b11800..365a869 100644 --- a/exercises/error_handling/errors5.rs +++ b/exercises/error_handling/errors5.rs @@ -43,8 +43,8 @@ impl PositiveNonzeroInteger { impl fmt::Display for CreationError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let description = match *self { - CreationError::Negative => "Number is negative", - CreationError::Zero => "Number is zero", + CreationError::Negative => "number is negative", + CreationError::Zero => "number is zero", }; f.write_str(description) } diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs index cee7250..0f6b27a 100644 --- a/exercises/error_handling/errors6.rs +++ b/exercises/error_handling/errors6.rs @@ -10,11 +10,20 @@ // I AM NOT DONE +use std::num::ParseIntError; + // This is a custom error type that we will be using in `parse_pos_nonzero()`. #[derive(PartialEq, Debug)] enum ParsePosNonzeroError { - CreationError, - ParseIntError + Creation(CreationError), + ParseInt(ParseIntError) +} + +impl ParsePosNonzeroError { + fn from_creation(err: CreationError) -> ParsePosNonzeroError { + ParsePosNonzeroError::Creation(err) + } + // TODO: add another error conversion function here. } fn parse_pos_nonzero(s: &str) @@ -24,7 +33,7 @@ fn parse_pos_nonzero(s: &str) // when `parse()` returns an error. let x: i64 = s.parse().unwrap(); PositiveNonzeroInteger::new(x) - .or(Err(ParsePosNonzeroError::CreationError)) + .map_err(ParsePosNonzeroError::from_creation) } // Don't change anything below this line. @@ -54,17 +63,18 @@ mod test { #[test] fn test_parse_error() { - assert_eq!( + // We can't construct a ParseIntError, so we have to pattern match. + assert!(matches!( parse_pos_nonzero("not a number"), - Err(ParsePosNonzeroError::ParseIntError) - ); + Err(ParsePosNonzeroError::ParseInt(_)) + )); } #[test] fn test_negative() { assert_eq!( parse_pos_nonzero("-555"), - Err(ParsePosNonzeroError::CreationError) + Err(ParsePosNonzeroError::Creation(CreationError::Negative)) ); } @@ -72,15 +82,14 @@ mod test { fn test_zero() { assert_eq!( parse_pos_nonzero("0"), - Err(ParsePosNonzeroError::CreationError) + Err(ParsePosNonzeroError::Creation(CreationError::Zero)) ); } #[test] fn test_positive() { - assert_eq!( - parse_pos_nonzero("42"), - Ok(PositiveNonzeroInteger(42)) - ); + let x = PositiveNonzeroInteger::new(42); + assert!(x.is_ok()); + assert_eq!(parse_pos_nonzero("42"), Ok(x.unwrap())); } } diff --git a/info.toml b/info.toml index 63eb78b..7695037 100644 --- a/info.toml +++ b/info.toml @@ -532,16 +532,19 @@ path = "exercises/error_handling/errors6.rs" mode = "test" hint = """ This exercise uses a completed version of `PositiveNonzeroInteger` from -the errors4. +errors4. -Below the TODO line, there is an example of using the `.or()` method -on a `Result` to transform one type of error into another. Try using -something similar on the `Result` from `parse()`. You might use the `?` -operator to return early from the function, or you might use a `match` -expression, or maybe there's another way! +Below the line that TODO asks you to change, there is an example of using +the `map_err()` method on a `Result` to transform one type of error into +another. Try using something similar on the `Result` from `parse()`. You +might use the `?` operator to return early from the function, or you might +use a `match` expression, or maybe there's another way! -Read more about `.or()` in the `std::result` documentation: -https://doc.rust-lang.org/std/result/enum.Result.html#method.or""" +You can create another function inside `impl ParsePosNonzeroError` to use +with `map_err()`. + +Read more about `map_err()` in the `std::result` documentation: +https://doc.rust-lang.org/std/result/enum.Result.html#method.map_err""" # Generics @@ -927,7 +930,7 @@ hint = """ Follow the steps provided right before the `TryFrom` implementation. You can also use the example at https://doc.rust-lang.org/std/convert/trait.TryFrom.html -You might want to look back at the exercise errorsn (or its hints) to remind +You might want to look back at the exercise errors5 (or its hints) to remind yourself about how `Box` works. If you're trying to return a string as an error, note that neither `str`