From aa693d7343f4747f067728a9c8bbcf8dc9b89e07 Mon Sep 17 00:00:00 2001 From: joshuacaffey Date: Wed, 7 Sep 2022 21:02:31 -0500 Subject: [PATCH] Update parse_pos_nonzero with alternate solution Since your repo is likely to be seen by lots of us learning rust, I thought I'd drop in the map_err alternative. The match you wrote was my first solution too, but this is cleaner / more readable. Thanks for sharing your solutions! --- exercises/error_handling/errors6.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs index 5dd3998..70b00b8 100644 --- a/exercises/error_handling/errors6.rs +++ b/exercises/error_handling/errors6.rs @@ -26,20 +26,25 @@ impl ParsePosNonzeroError { } } -fn parse_pos_nonzero(s: &str) - -> Result +fn parse_pos_nonzero(s: &str) -> Result { - match s.parse() { - Ok(n) => { - match PositiveNonzeroInteger::new(n) { - Ok(n) => Ok(n), - Err(err) => Err(ParsePosNonzeroError::from_creation(err)), - } - } - Err(err) => Err(ParsePosNonzeroError::from_parse_int(err)), - } + // long way + // match s.parse() { + // Ok(n) => { + // match PositiveNonzeroInteger::new(n) { + // Ok(n) => Ok(n), + // Err(err) => Err(ParsePosNonzeroError::from_creation(err)), + // } + // } + // Err(err) => Err(ParsePosNonzeroError::from_parse_int(err)), + // } + + // short way + let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; + PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation) } + // Don't change anything below this line. #[derive(PartialEq, Debug)]