diff options
| author | mo8it <mo8it@proton.me> | 2024-08-22 14:37:47 +0200 |
|---|---|---|
| committer | mo8it <mo8it@proton.me> | 2024-08-22 14:37:47 +0200 |
| commit | 423b50b068f7cb489e4c5f241b696491419620c1 (patch) | |
| tree | 42641eb150da8af0db5b289a81ae4bb182992e24 /solutions/13_error_handling/errors4.rs | |
| parent | bedf0789f2129f333cc1af14775c40d7312297f5 (diff) | |
Use match instead of comparison chain
Diffstat (limited to 'solutions/13_error_handling/errors4.rs')
| -rw-r--r-- | solutions/13_error_handling/errors4.rs | 12 |
1 files changed, 4 insertions, 8 deletions
diff --git a/solutions/13_error_handling/errors4.rs b/solutions/13_error_handling/errors4.rs index f4d39bf..7f176cf 100644 --- a/solutions/13_error_handling/errors4.rs +++ b/solutions/13_error_handling/errors4.rs @@ -1,5 +1,3 @@ -#![allow(clippy::comparison_chain)] - #[derive(PartialEq, Debug)] enum CreationError { Negative, @@ -11,12 +9,10 @@ struct PositiveNonzeroInteger(u64); impl PositiveNonzeroInteger { fn new(value: i64) -> Result<Self, CreationError> { - if value == 0 { - Err(CreationError::Zero) - } else if value < 0 { - Err(CreationError::Negative) - } else { - Ok(Self(value as u64)) + match value.cmp(&0) { + Ordering::Less => Err(CreationError::Negative), + Ordering::Equal => Err(CreationError::Zero), + Ordering::Greater => Ok(Self(value as u64)), } } } |
