Skip to content

Commit 423b50b

Browse files
committed
Use match instead of comparison chain
1 parent bedf078 commit 423b50b

File tree

2 files changed

+4
-10
lines changed

2 files changed

+4
-10
lines changed

exercises/13_error_handling/errors4.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(clippy::comparison_chain)]
2-
31
#[derive(PartialEq, Debug)]
42
enum CreationError {
53
Negative,

solutions/13_error_handling/errors4.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
#![allow(clippy::comparison_chain)]
2-
31
#[derive(PartialEq, Debug)]
42
enum CreationError {
53
Negative,
@@ -11,12 +9,10 @@ struct PositiveNonzeroInteger(u64);
119

1210
impl PositiveNonzeroInteger {
1311
fn new(value: i64) -> Result<Self, CreationError> {
14-
if value == 0 {
15-
Err(CreationError::Zero)
16-
} else if value < 0 {
17-
Err(CreationError::Negative)
18-
} else {
19-
Ok(Self(value as u64))
12+
match value.cmp(&0) {
13+
Ordering::Less => Err(CreationError::Negative),
14+
Ordering::Equal => Err(CreationError::Zero),
15+
Ordering::Greater => Ok(Self(value as u64)),
2016
}
2117
}
2218
}

0 commit comments

Comments
 (0)