errors3.rs (923B)
1 // This is a program that is trying to use a completed version of the 2 // `total_cost` function from the previous exercise. It's not working though! 3 // Why not? What should we do to fix it? 4 5 use std::num::ParseIntError; 6 7 // Don't change this function. 8 fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> { 9 let processing_fee = 1; 10 let cost_per_item = 5; 11 let qty = item_quantity.parse::<i32>()?; 12 13 Ok(qty * cost_per_item + processing_fee) 14 } 15 16 // TODO: Fix the compiler error by changing the signature and body of the 17 // `main` function. 18 fn main() -> Result<(), ParseIntError> { 19 let mut tokens = 100; 20 let pretend_user_input = "8"; 21 22 // Don't change this line. 23 let cost = total_cost(pretend_user_input)?; 24 25 if cost > tokens { 26 println!("You can't afford that many!"); 27 } else { 28 tokens -= cost; 29 println!("You now have {tokens} tokens."); 30 } 31 32 Ok(()) 33 }