From fdc1801fee8a04ba8da91974efe2e1938d9bdcc4 Mon Sep 17 00:00:00 2001 From: Philip Tenteromano Date: Wed, 26 Apr 2023 13:55:47 -0400 Subject: [PATCH] through errors --- exercises/error_handling/errors2.rs | 4 +--- exercises/error_handling/errors3.rs | 6 +++--- exercises/error_handling/errors4.rs | 9 +++++---- exercises/error_handling/errors5.rs | 4 +--- 4 files changed, 10 insertions(+), 13 deletions(-) diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs index 1cd8fc66..0bd0530a 100644 --- a/exercises/error_handling/errors2.rs +++ b/exercises/error_handling/errors2.rs @@ -17,14 +17,12 @@ // one is a lot shorter! // Execute `rustlings hint errors2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::num::ParseIntError; pub fn total_cost(item_quantity: &str) -> Result { let processing_fee = 1; let cost_per_item = 5; - let qty = item_quantity.parse::(); + let qty = item_quantity.parse::()?; Ok(qty * cost_per_item + processing_fee) } diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs index a2d2d190..b668f50e 100644 --- a/exercises/error_handling/errors3.rs +++ b/exercises/error_handling/errors3.rs @@ -4,11 +4,9 @@ // Why not? What should we do to fix it? // Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - use std::num::ParseIntError; -fn main() { +fn main() -> Result<(), ParseIntError> { let mut tokens = 100; let pretend_user_input = "8"; @@ -20,6 +18,8 @@ fn main() { tokens -= cost; println!("You now have {} tokens.", tokens); } + + Ok(()) } pub fn total_cost(item_quantity: &str) -> Result { diff --git a/exercises/error_handling/errors4.rs b/exercises/error_handling/errors4.rs index 0efe8ccd..42f4be62 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,11 @@ enum CreationError { impl PositiveNonzeroInteger { fn new(value: i64) -> Result { - // Hmm...? Why is this only returning an Ok value? - Ok(PositiveNonzeroInteger(value as u64)) + match value { + 0 => Err(CreationError::Zero), + _ if value < 0 => Err(CreationError::Negative), + _ => 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)?);