mirror of
https://github.com/rust-lang/rustlings.git
synced 2026-01-11 13:19:18 +00:00
using MATCH AND ELSE
This commit is contained in:
parent
938bf326df
commit
6c2f6772e6
@ -4,7 +4,7 @@
|
|||||||
// Why not? What should we do to fix it?
|
// Why not? What should we do to fix it?
|
||||||
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint.
|
// Execute `rustlings hint errors3` or use the `hint` watch subcommand for a hint.
|
||||||
|
|
||||||
// I AM NOT DONE
|
|
||||||
|
|
||||||
use std::num::ParseIntError;
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
@ -12,16 +12,24 @@ fn main() {
|
|||||||
let mut tokens = 100;
|
let mut tokens = 100;
|
||||||
let pretend_user_input = "8";
|
let pretend_user_input = "8";
|
||||||
|
|
||||||
let cost = total_cost(pretend_user_input)?;
|
//let cost = total_cost(pretend_user_input)?;
|
||||||
|
|
||||||
|
match total_cost(pretend_user_input) {
|
||||||
|
Ok(cost) => {
|
||||||
if cost > tokens {
|
if cost > tokens {
|
||||||
println!("You can't afford that many!");
|
println!("You can't afford that many!");
|
||||||
} else {
|
} else {
|
||||||
tokens -= cost;
|
tokens -= cost;
|
||||||
println!("You now have {} tokens.", tokens);
|
println!("You now have {} tokens.", tokens);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Err(err) => {
|
||||||
|
println!("Error: {}", err);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||||
let processing_fee = 1;
|
let processing_fee = 1;
|
||||||
let cost_per_item = 5;
|
let cost_per_item = 5;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user