From 5a9c46b51eb284084c992279c4bde828d55fad62 Mon Sep 17 00:00:00 2001 From: Rock070 Date: Sun, 31 Dec 2023 04:02:19 +0800 Subject: [PATCH] Fix error handling in PositiveNonzeroInteger::new() --- exercises/13_error_handling/errors4.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index d6d6fcb6..c2d0646b 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -3,8 +3,6 @@ // Execute `rustlings hint errors4` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE - #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -17,7 +15,13 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { // Hmm... Why is this always returning an Ok value? - Ok(PositiveNonzeroInteger(value as u64)) + if value == 0 { + Err(CreationError::Zero) + } else if value < 0 { + Err(CreationError::Negative) + } else { + Ok(PositiveNonzeroInteger(value as u64)) + } } }