File tree Expand file tree Collapse file tree 2 files changed +4
-10
lines changed
exercises/13_error_handling
solutions/13_error_handling Expand file tree Collapse file tree 2 files changed +4
-10
lines changed Original file line number Diff line number Diff line change 1
- #![ allow( clippy:: comparison_chain) ]
2
-
3
1
#[ derive( PartialEq , Debug ) ]
4
2
enum CreationError {
5
3
Negative ,
Original file line number Diff line number Diff line change 1
- #![ allow( clippy:: comparison_chain) ]
2
-
3
1
#[ derive( PartialEq , Debug ) ]
4
2
enum CreationError {
5
3
Negative ,
@@ -11,12 +9,10 @@ struct PositiveNonzeroInteger(u64);
11
9
12
10
impl PositiveNonzeroInteger {
13
11
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 ) ) ,
20
16
}
21
17
}
22
18
}
You can’t perform that action at this time.
0 commit comments