Skip to content

Commit dd0634c

Browse files
authored
Merge pull request rust-lang#2158 from mnshdw/mnshdw/feedback-errors6
errors6: Add alternative solution using From trait
2 parents 38016cb + fc0cd8f commit dd0634c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

solutions/13_error_handling/errors6.rs

+15
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,21 @@ impl ParsePosNonzeroError {
2929
}
3030
}
3131

32+
// As an alternative solution, implementing the `From` trait allows for the
33+
// automatic conversion from a `ParseIntError` into a `ParsePosNonzeroError`
34+
// using the `?` operator, without the need to call `map_err`.
35+
//
36+
// ```
37+
// let x: i64 = s.parse()?;
38+
// ```
39+
//
40+
// Traits like `From` will be dealt with in later exercises.
41+
impl From<ParseIntError> for ParsePosNonzeroError {
42+
fn from(err: ParseIntError) -> Self {
43+
ParsePosNonzeroError::ParseInt(err)
44+
}
45+
}
46+
3247
#[derive(PartialEq, Debug)]
3348
struct PositiveNonzeroInteger(u64);
3449

0 commit comments

Comments
 (0)