Refactor total_cost function to handle parsing errors

This commit is contained in:
Rock070 2023-12-31 03:45:53 +08:00
parent 844dff4902
commit 77fd6a3a1b

View File

@ -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<i32, ParseIntError> {
@ -28,7 +26,11 @@ pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>();
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)]