Update errors6.rs

This commit is contained in:
Nielsen 2024-01-19 11:40:46 +01:00
parent 9ca4d9f5e4
commit 6279d2b04c

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()`.
@ -34,7 +32,16 @@ impl ParsePosNonzeroError {
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();
// Solution 1:
// let x: i64 = match s.parse::<i64>() {
// 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)
}