using MATCH AND ELSE

This commit is contained in:
Hariettemaina 2023-05-22 17:03:38 +03:00
parent 938bf326df
commit 6c2f6772e6

View File

@ -4,7 +4,7 @@
// 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;
@ -12,16 +12,24 @@ fn main() {
let mut tokens = 100;
let pretend_user_input = "8";
let cost = total_cost(pretend_user_input)?;
//let cost = total_cost(pretend_user_input)?;
if cost > tokens {
println!("You can't afford that many!");
} else {
tokens -= cost;
println!("You now have {} tokens.", tokens);
match total_cost(pretend_user_input) {
Ok(cost) => {
if cost > tokens {
println!("You can't afford that many!");
} else {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}
}
Err(err) => {
println!("Error: {}", err);
}
}
}
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;