Skip to content

Commit

Permalink
Merge pull request rust-lang#2158 from mnshdw/mnshdw/feedback-errors6
Browse files Browse the repository at this point in the history
errors6: Add alternative solution using From trait
  • Loading branch information
mo8it authored Nov 14, 2024
2 parents 38016cb + fc0cd8f commit dd0634c
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 dd0634c

Please sign in to comment.