rustlings

solving rustlings ft. dracuxan
git clone [email protected]:dracuxan/rustlings.git
Log | Files | Refs

errors3.rs (919B)


      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 fn main() -> Result<(), ParseIntError> {
     17     //    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ added
     18     let mut tokens = 100;
     19     let pretend_user_input = "8";
     20 
     21     let cost = total_cost(pretend_user_input)?;
     22 
     23     if cost > tokens {
     24         println!("You can't afford that many!");
     25     } else {
     26         tokens -= cost;
     27         println!("You now have {tokens} tokens.");
     28     }
     29 
     30     // Added this line to return the `Ok` variant of the expected `Result`.
     31     Ok(())
     32 }