From 6c2f6772e6e8d9955a8b33a8ffa6ebc10e0e8a26 Mon Sep 17 00:00:00 2001 From: Hariettemaina Date: Mon, 22 May 2023 17:03:38 +0300 Subject: [PATCH] using MATCH AND ELSE --- exercises/error_handling/errors3.rs | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs index a2d2d190..04c9be30 100644 --- a/exercises/error_handling/errors3.rs +++ b/exercises/error_handling/errors3.rs @@ -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 { let processing_fee = 1; let cost_per_item = 5;