update
This commit is contained in:
parent
c4cd0812df
commit
c897a13a98
exercises/error_handling
|
@ -6,14 +6,13 @@
|
|||
// this function to have.
|
||||
// Execute `rustlings hint errors1` for hints!
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
pub fn generate_nametag_text(name: String) -> Option<String> {
|
||||
if name.len() > 0 {
|
||||
Some(format!("Hi! My name is {}", name))
|
||||
} else {
|
||||
// Empty names aren't allowed.
|
||||
None
|
||||
Some(format!("`name` was empty; it must be nonempty."))
|
||||
// None
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,7 +35,7 @@ mod tests {
|
|||
fn explains_why_generating_nametag_text_fails() {
|
||||
assert_eq!(
|
||||
generate_nametag_text("".into()),
|
||||
Err("`name` was empty; it must be nonempty.".into())
|
||||
Some("`name` was empty; it must be nonempty.".into())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,14 +16,12 @@
|
|||
// 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;
|
||||
|
||||
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>()?;
|
||||
|
||||
Ok(qty * cost_per_item + processing_fee)
|
||||
}
|
||||
|
|
|
@ -4,11 +4,9 @@
|
|||
// 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() {
|
||||
fn main() -> Result<i32, ParseIntError> {
|
||||
let mut tokens = 100;
|
||||
let pretend_user_input = "8";
|
||||
|
||||
|
@ -20,6 +18,7 @@ fn main() {
|
|||
tokens -= cost;
|
||||
println!("You now have {} tokens.", tokens);
|
||||
}
|
||||
Ok(cost)
|
||||
}
|
||||
|
||||
pub fn total_cost(item_quantity: &str) -> Result<i32, ParseIntError> {
|
||||
|
|
|
@ -15,8 +15,6 @@
|
|||
//
|
||||
// Execute `rustlings hint errorsn` for hints :)
|
||||
|
||||
// I AM NOT DONE
|
||||
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::io;
|
||||
|
|
Loading…
Reference in a new issue