This commit is contained in:
blacktoast 2021-10-14 06:10:56 +00:00
parent 56a07c5c4b
commit b9022cfd1f
2 changed files with 15 additions and 10 deletions

View File

@ -16,7 +16,6 @@
// There are at least two ways to implement this that are both correct-- but
// one is a lot shorter! Execute `rustlings hint errors2` for hints to both ways.
// I AM NOT DONE
use std::num::ParseIntError;
@ -24,8 +23,10 @@ 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>();
Ok(qty * cost_per_item + processing_fee)
match qty{
Ok(qty)=> Ok(qty * cost_per_item + processing_fee),
Err(error)=> Err(error)
}
}
#[cfg(test)]

View File

@ -4,16 +4,18 @@
// Why not? What should we do to fix it?
// Execute `rustlings hint errors3` for hints!
// I AM NOT DONE
use std::num::ParseIntError;
fn main() {
let mut tokens = 100;
let pretend_user_input = "8";
let cost = total_cost(pretend_user_input)?;
let pretend_user_input = "a";
let cost = total_cost(pretend_user_input);
let cost= match cost {
Ok(cost)=>cost,
Err(err)=>err
};
if cost > tokens {
println!("You can't afford that many!");
} else {
@ -25,7 +27,9 @@ fn main() {
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>()?;
Ok(qty * cost_per_item + processing_fee)
let qty = item_quantity.parse::<i32>();
match qty{
Ok(qty)=> Ok(qty * cost_per_item + processing_fee),
Err(error)=> Err(error)
}
}