mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-07 03:09:19 +00:00
Update errors6.rs
This commit is contained in:
parent
9ca4d9f5e4
commit
6279d2b04c
@ -9,8 +9,6 @@
|
|||||||
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors6` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
// This is a custom error type that we will be using in `parse_pos_nonzero()`.
|
// 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> {
|
fn parse_pos_nonzero(s: &str) -> Result<PositiveNonzeroInteger, ParsePosNonzeroError> {
|
||||||
// TODO: change this to return an appropriate error instead of panicking
|
// TODO: change this to return an appropriate error instead of panicking
|
||||||
// when `parse()` returns an error.
|
// 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)
|
PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user