From c897a13a988839bcf329926bd540144d80079766 Mon Sep 17 00:00:00 2001 From: tab <dearhange@126.com> Date: Tue, 5 May 2020 23:09:21 +0800 Subject: [PATCH] update --- exercises/error_handling/errors1.rs | 7 +++---- exercises/error_handling/errors2.rs | 4 +--- exercises/error_handling/errors3.rs | 5 ++--- exercises/error_handling/errorsn.rs | 2 -- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/exercises/error_handling/errors1.rs b/exercises/error_handling/errors1.rs index 9c24d85..dff7798 100644 --- a/exercises/error_handling/errors1.rs +++ b/exercises/error_handling/errors1.rs @@ -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()) ); } } diff --git a/exercises/error_handling/errors2.rs b/exercises/error_handling/errors2.rs index aad3a93..478e3e4 100644 --- a/exercises/error_handling/errors2.rs +++ b/exercises/error_handling/errors2.rs @@ -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) } diff --git a/exercises/error_handling/errors3.rs b/exercises/error_handling/errors3.rs index 460ac5c..1f8ed0e 100644 --- a/exercises/error_handling/errors3.rs +++ b/exercises/error_handling/errors3.rs @@ -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> { diff --git a/exercises/error_handling/errorsn.rs b/exercises/error_handling/errorsn.rs index 1b985bf..cf605c5 100644 --- a/exercises/error_handling/errorsn.rs +++ b/exercises/error_handling/errorsn.rs @@ -15,8 +15,6 @@ // // Execute `rustlings hint errorsn` for hints :) -// I AM NOT DONE - use std::error; use std::fmt; use std::io;