Skip to content

Commit

Permalink
Add alternative solution using From trait
Browse files Browse the repository at this point in the history
  • Loading branch information
mnshdw committed Nov 13, 2024
1 parent 38016cb commit d5cae8f
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions solutions/13_error_handling/errors6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ impl ParsePosNonzeroError {
}
}

/// As an alternative solution, implementing the `From` trait allows for the
/// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
/// using the `?` operator, without the need to call `map_err`.
///
/// ```
/// let x: i64 = s.parse()?;
/// ```
///
/// Traits like `From` will be dealt with in later exercises.
impl From<ParseIntError> for ParsePosNonzeroError {
fn from(err: ParseIntError) -> Self {
ParsePosNonzeroError::ParseInt(err)
}
}

#[derive(PartialEq, Debug)]
struct PositiveNonzeroInteger(u64);

Expand Down

0 comments on commit d5cae8f

Please sign in to comment.