From 6279d2b04c4d2f84aa5561a0c0f60a6a944f0eb9 Mon Sep 17 00:00:00 2001 From: Nielsen Date: Fri, 19 Jan 2024 11:40:46 +0100 Subject: [PATCH] Update errors6.rs --- exercises/13_error_handling/errors6.rs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/exercises/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs index 5faab803..3cc108d3 100644 --- a/exercises/13_error_handling/errors6.rs +++ b/exercises/13_error_handling/errors6.rs @@ -9,8 +9,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()`. @@ -34,7 +32,16 @@ impl ParsePosNonzeroError { 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(); + + // Solution 1: + // let x: i64 = match s.parse::() { + // Ok(x) => x, + // Err(e) => return Err(ParsePosNonzeroError::ParseInt(e)), + // }; + + // Solution 2: + let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; + PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation) }