From 28c993afe1255057da5ac844f0cd68c8eed22561 Mon Sep 17 00:00:00 2001 From: Phan Huy Hoang Date: Mon, 9 Jan 2023 10:14:20 +0700 Subject: [PATCH] [Add] exercises/error_handling --- exercises/error_handling/errors2.rs | 2 +- exercises/error_handling/errors4.rs | 11 +++++++---- exercises/error_handling/errors5.rs | 4 +--- exercises/error_handling/errors6.rs | 11 ++++------- 4 files changed, 13 insertions(+), 15 deletions(-) diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs index f581de44..0bd0530a 100644 --- a/exercises/error_handling/errors2.rs +++ b/exercises/error_handling/errors2.rs @@ -24,7 +24,7 @@ pub fn total_cost(item_quantity: &str) -> Result { let cost_per_item = 5; let qty = item_quantity.parse::()?; - Ok(qty * cost_per_item + processing_fee.ok) + Ok(qty * cost_per_item + processing_fee) } #[cfg(test)] diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs index 0efe8ccd..e23b32c4 100644 --- a/exercises/error_handling/errors4.rs +++ b/exercises/error_handling/errors4.rs @@ -1,8 +1,6 @@ // errors4.rs // Execute `rustlings hint errors4` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[derive(PartialEq, Debug)] struct PositiveNonzeroInteger(u64); @@ -14,8 +12,13 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { - // Hmm...? Why is this only returning an Ok value? - Ok(PositiveNonzeroInteger(value as u64)) + // // Hmm...? Why is this only returning an Ok value? + // Ok(PositiveNonzeroInteger(value as u64)) + match value { + x if x < 0 => Err(CreationError::Negative), + 0 => Err(CreationError::Zero), + _ => Ok(PositiveNonzeroInteger(value as u64)) + } } } diff --git a/exercises/error_handling/errors5.rs b/exercises/error_handling/errors5.rs index 6da06ef3..f93f705c 100644 --- a/exercises/error_handling/errors5.rs +++ b/exercises/error_handling/errors5.rs @@ -16,14 +16,12 @@ // Execute `rustlings hint errors5` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::error; use std::fmt; use std::num::ParseIntError; // TODO: update the return type of `main()` to make this compile. -fn main() -> Result<(), Box> { +fn main() -> Result<(), Box> { let pretend_user_input = "42"; let x: i64 = pretend_user_input.parse()?; println!("output={:?}", PositiveNonzeroInteger::new(x)?); diff --git a/exercises/error_handling/errors6.rs b/exercises/error_handling/errors6.rs index 8097b490..991af297 100644 --- a/exercises/error_handling/errors6.rs +++ b/exercises/error_handling/errors6.rs @@ -8,8 +8,6 @@ // Execute `rustlings hint errors6` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::num::ParseIntError; // This is a custom error type that we will be using in `parse_pos_nonzero()`. @@ -23,14 +21,13 @@ impl ParsePosNonzeroError { fn from_creation(err: CreationError) -> ParsePosNonzeroError { ParsePosNonzeroError::Creation(err) } - // TODO: add another error conversion function here. - // fn from_parseint... + fn from_parseint (err: ParseIntError) -> ParsePosNonzeroError { + ParsePosNonzeroError::ParseInt(err) + } } fn parse_pos_nonzero(s: &str) -> Result { - // TODO: change this to return an appropriate error instead of panicking - // when `parse()` returns an error. - let x: i64 = s.parse().unwrap(); + let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; PositiveNonzeroInteger::new(x).map_err(ParsePosNonzeroError::from_creation) }