mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-06 18:59:19 +00:00
Refactor total_cost function to handle parsing errors
This commit is contained in:
parent
844dff4902
commit
77fd6a3a1b
@ -19,8 +19,6 @@
|
|||||||
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
|
// Execute `rustlings hint errors2` or use the `hint` watch subcommand for a
|
||||||
// hint.
|
// hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
pub fn total_cost(item_quantity: &str) -> Result<i32, 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 cost_per_item = 5;
|
||||||
let qty = item_quantity.parse::<i32>();
|
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)]
|
#[cfg(test)]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user