From b9022cfd1ffbb24df01f256acc45c19d4b31ab3d Mon Sep 17 00:00:00 2001 From: blacktoast Date: Thu, 14 Oct 2021 06:10:56 +0000 Subject: [PATCH] error 3 --- exercises/error_handling/errors2.rs | 7 ++++--- exercises/error_handling/errors3.rs | 18 +++++++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs index aad3a93f..8eea37b7 100644 --- a/exercises/error_handling/errors2.rs +++ b/exercises/error_handling/errors2.rs @@ -16,7 +16,6 @@ // There are at least two ways to implement this that are both correct-- but // one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways. -// I AM NOT DONE use std::num::ParseIntError; @@ -24,8 +23,10 @@ pub fn total_cost(item_quantity: &str) -> Result { let processing_fee = 1; let cost_per_item = 5; let qty = item_quantity.parse::(); - - Ok(qty * cost_per_item + processing_fee) + match qty{ + Ok(qty)=> Ok(qty * cost_per_item + processing_fee), + Err(error)=> Err(error) + } } #[cfg(test)] diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs index 460ac5c4..b4929e75 100644 --- a/exercises/error_handling/errors3.rs +++ b/exercises/error_handling/errors3.rs @@ -4,16 +4,18 @@ // Why not? What should we do to fix it? // Execute `rustlings hint errors3` for hints! -// I AM NOT DONE use std::num::ParseIntError; fn main() { let mut tokens = 100; - let pretend_user_input = "8"; - - let cost = total_cost(pretend_user_input)?; + let pretend_user_input = "a"; + let cost = total_cost(pretend_user_input); + let cost= match cost { + Ok(cost)=>cost, + Err(err)=>err + }; if cost > tokens { println!("You can't afford that many!"); } else { @@ -25,7 +27,9 @@ fn main() { pub fn total_cost(item_quantity: &str) -> Result { let processing_fee = 1; let cost_per_item = 5; - let qty = item_quantity.parse::()?; - - Ok(qty * cost_per_item + processing_fee) + let qty = item_quantity.parse::(); + match qty{ + Ok(qty)=> Ok(qty * cost_per_item + processing_fee), + Err(error)=> Err(error) + } }