From 77fd6a3a1bb47b5cd92b5170da4b734670750e0f Mon Sep 17 00:00:00 2001 From: Rock070 Date: Sun, 31 Dec 2023 03:45:53 +0800 Subject: [PATCH] Refactor total_cost function to handle parsing errors --- exercises/13_error_handling/errors2.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs index 631fe67f..5fede0df 100644 --- a/exercises/13_error_handling/errors2.rs +++ b/exercises/13_error_handling/errors2.rs @@ -19,8 +19,6 @@ // 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 { @@ -28,7 +26,11 @@ 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) + let qty = match qty { + Ok(number)=> Ok(number * cost_per_item + processing_fee), + Err(err) => Err(err) + }; + qty } #[cfg(test)]