Skip to content

Commit

Permalink
finish errors4
Browse files Browse the repository at this point in the history
  • Loading branch information
cyz-ing committed Jan 27, 2025
1 parent 1e32a42 commit 9a6585d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
2 changes: 1 addition & 1 deletion exercises/error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
Err(err)=>Err(err)

}

}
7 changes: 5 additions & 2 deletions exercises/error_handling/errors4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ enum CreationError {

impl PositiveNonzeroInteger {
fn new(value: i64) -> Result<PositiveNonzeroInteger, CreationError> {
// Hmm...? Why is this only returning an Ok value?
Ok(PositiveNonzeroInteger(value as u64))
match value {
x if x < 0 => Err(CreationError::Negative),
0 => Err(CreationError::Zero),
x => Ok(PositiveNonzeroInteger(x as u64)),
}
}
}

Expand Down

0 comments on commit 9a6585d

Please sign in to comment.