From 8de108505d807666584a1654e2b5e940ede0d81f Mon Sep 17 00:00:00 2001 From: blacktoast Date: Thu, 14 Oct 2021 07:42:54 +0000 Subject: [PATCH] =?UTF-8?q?=EC=97=90=EB=9F=AC=205=EB=B2=88=20=ED=91=B8?= =?UTF-8?q?=EB=8A=94=EC=A4=91..?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/error_handling/errors4.rs | 8 ++++++-- exercises/error_handling/errors5.rs | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs index 0685c374..eeba3a9c 100644 --- a/exercises/error_handling/errors4.rs +++ b/exercises/error_handling/errors4.rs @@ -1,7 +1,6 @@ // errors4.rs // Make this test pass! Execute `rustlings hint errors4` for hints :) -// I AM NOT DONE #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -14,7 +13,12 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { - Ok(PositiveNonzeroInteger(value as u64)) + match value{ + (value) if value>0 =>Ok(PositiveNonzeroInteger(value as u64)), + (value) if value==0=>Err(CreationError::Zero), + (value) if value < 0=>Err(CreationError::Negative), + _=>Err(panic!("no")) + } } } diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs index 365a8691..5a1f381c 100644 --- a/exercises/error_handling/errors5.rs +++ b/exercises/error_handling/errors5.rs @@ -11,7 +11,7 @@ use std::fmt; use std::num::ParseIntError; // TODO: update the return type of `main()` to make this compile. -fn main() -> Result<(), ParseIntError> { +fn main() -> Result<(), (ParseIntError,CreationError)> { let pretend_user_input = "42"; let x: i64 = pretend_user_input.parse()?; println!("output={:?}", PositiveNonzeroInteger::new(x)?);