This commit is contained in:
2024-04-10 14:36:38 -04:00
parent be78a78343
commit c682aa9c6c
16 changed files with 72 additions and 57 deletions

View File

@@ -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()`.
@@ -25,14 +23,23 @@ impl ParsePosNonzeroError {
ParsePosNonzeroError::Creation(err)
}
// 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<PositiveNonzeroInteger, ParsePosNonzeroError> {
// 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).map_err(ParsePosNonzeroError::from_creation)
let x = s.parse();
match x {
Ok(num) => {
PositiveNonzeroInteger::new(num).map_err(ParsePosNonzeroError::from_creation)
},
Err(e) => {
return Err(ParsePosNonzeroError::ParseInt(e));
}
}
}
// Don't change anything below this line.