mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-10 12:49:18 +00:00
error 3
This commit is contained in:
parent
56a07c5c4b
commit
b9022cfd1f
@ -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)]
|
||||
|
||||
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user