mirror of
https://github.com/rust-lang/rustlings.git
synced 2025-12-28 06:49:19 +00:00
Merge c6c6d272324a2cae4b4d6f4a145c43c180a3bcd8 into b5d440fdc3a1fadad6dc6196dad2acddabdc671f
This commit is contained in:
commit
f0bdef1f70
@ -19,15 +19,6 @@ enum ParsePosNonzeroError {
|
|||||||
ParseInt(ParseIntError),
|
ParseInt(ParseIntError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParsePosNonzeroError {
|
|
||||||
fn from_creation(err: CreationError) -> Self {
|
|
||||||
Self::Creation(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Add another error conversion function here.
|
|
||||||
// fn from_parse_int(???) -> Self { ??? }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Debug)]
|
#[derive(PartialEq, Debug)]
|
||||||
struct PositiveNonzeroInteger(u64);
|
struct PositiveNonzeroInteger(u64);
|
||||||
|
|
||||||
@ -44,7 +35,7 @@ impl PositiveNonzeroInteger {
|
|||||||
// 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();
|
let x: i64 = s.parse().unwrap();
|
||||||
Self::new(x).map_err(ParsePosNonzeroError::from_creation)
|
Self::new(x).map_err(ParsePosNonzeroError::Creation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -19,16 +19,6 @@ enum ParsePosNonzeroError {
|
|||||||
ParseInt(ParseIntError),
|
ParseInt(ParseIntError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ParsePosNonzeroError {
|
|
||||||
fn from_creation(err: CreationError) -> Self {
|
|
||||||
Self::Creation(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_parse_int(err: ParseIntError) -> Self {
|
|
||||||
Self::ParseInt(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// As an alternative solution, implementing the `From` trait allows for the
|
// As an alternative solution, implementing the `From` trait allows for the
|
||||||
// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
|
// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
|
||||||
// using the `?` operator, without the need to call `map_err`.
|
// using the `?` operator, without the need to call `map_err`.
|
||||||
@ -59,9 +49,9 @@ impl PositiveNonzeroInteger {
|
|||||||
fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
|
fn parse(s: &str) -> Result<Self, ParsePosNonzeroError> {
|
||||||
// Return an appropriate error instead of panicking when `parse()`
|
// Return an appropriate error instead of panicking when `parse()`
|
||||||
// returns an error.
|
// returns an error.
|
||||||
let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parse_int)?;
|
let x: i64 = s.parse().map_err(ParsePosNonzeroError::ParseInt)?;
|
||||||
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
Self::new(x).map_err(ParsePosNonzeroError::from_creation)
|
Self::new(x).map_err(ParsePosNonzeroError::Creation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user