From 423b50b068f7cb489e4c5f241b696491419620c1 Mon Sep 17 00:00:00 2001 From: mo8it Date: Thu, 22 Aug 2024 14:37:47 +0200 Subject: [PATCH] Use match instead of comparison chain --- exercises/13_error_handling/errors4.rs | 2 -- solutions/13_error_handling/errors4.rs | 12 ++++-------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index e41d5945..ba01e54b 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -1,5 +1,3 @@ -#![allow(clippy::comparison_chain)] - #[derive(PartialEq, Debug)] enum CreationError { Negative, diff --git a/solutions/13_error_handling/errors4.rs b/solutions/13_error_handling/errors4.rs index f4d39bf9..7f176cfc 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 { - 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)), } } }