Skip to content

Commit

Permalink
Add errors6
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldww committed May 2, 2023
1 parent dbf9f07 commit e7f7d48
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions exercises/error_handling/errors6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,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()`.
Expand All @@ -24,15 +22,18 @@ impl ParsePosNonzeroError {
ParsePosNonzeroError::Creation(err)
}
// TODO: add another error conversion function here.
fn from_parseint(err: ParseIntError) -> ParseIntError {
ParseIntError::ParseInt(err);
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();
let x: i64 = match s.parse::<i64>() {
Err(err) => return Err(ParsePosNonzeroError::ParseInt(err)),
Ok(n) => n
};
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
}

Expand Down

0 comments on commit e7f7d48

Please sign in to comment.