Skip to content

Commit

Permalink
finish errors3
Browse files Browse the repository at this point in the history
  • Loading branch information
cyz-ing committed Jan 27, 2025
1 parent b8886fe commit 1e32a42
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions exercises/error_handling/errors3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use std::num::ParseIntError;

fn main() {
fn main() -> Result<(), ParseIntError> {
let mut tokens = 100;
let pretend_user_input = "8";

Expand All @@ -23,12 +23,20 @@ fn main() {
tokens -= cost;
println!("You now have {} tokens.", tokens);
}
Ok(())
}

pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
let processing_fee = 1;
let cost_per_item = 5;
let qty = item_quantity.parse::<i32>()?;
let qty = item_quantity.parse::<i32>();
match qty{
Ok(quantity) => {
let total_cost=(quantity * cost_per_item + processing_fee);
Ok(total_cost)
}
Err(err)=>Err(err)

}

Ok(qty * cost_per_item + processing_fee)
}

0 comments on commit 1e32a42

Please sign in to comment.