From e5630438f2e970958aea062fc25c9fb73a3b4d3d Mon Sep 17 00:00:00 2001 From: jiangker Date: Thu, 11 Jul 2024 14:48:30 +0800 Subject: [PATCH] =?UTF-8?q?coding=2012=E3=80=8113?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/12_options/options1.rs | 6 ++++-- exercises/12_options/options2.rs | 4 ++-- exercises/12_options/options3.rs | 2 +- exercises/13_error_handling/errors1.rs | 6 +++--- exercises/13_error_handling/errors2.rs | 6 ++++-- exercises/13_error_handling/errors3.rs | 2 +- exercises/13_error_handling/errors4.rs | 9 +++++++-- exercises/13_error_handling/errors5.rs | 2 +- exercises/13_error_handling/errors6.rs | 4 ++-- 9 files changed, 25 insertions(+), 16 deletions(-) diff --git a/exercises/12_options/options1.rs b/exercises/12_options/options1.rs index 99648078..d2330f85 100644 --- a/exercises/12_options/options1.rs +++ b/exercises/12_options/options1.rs @@ -3,7 +3,9 @@ // someone eats it all, so no icecream is left (value 0). Return `None` if // `hour_of_day` is higher than 23. fn maybe_icecream(hour_of_day: u16) -> Option { - // TODO: Complete the function body. + if hour_of_day < 22 { Some(5) } + else if hour_of_day < 24 { Some(0) } + else { None } } fn main() { @@ -18,7 +20,7 @@ mod tests { fn raw_value() { // TODO: Fix this test. How do you get the value contained in the // Option? - let icecreams = maybe_icecream(12); + let icecreams = maybe_icecream(12).unwrap(); assert_eq!(icecreams, 5); // Don't change this line. } diff --git a/exercises/12_options/options2.rs b/exercises/12_options/options2.rs index 07c27c6e..1039d61d 100644 --- a/exercises/12_options/options2.rs +++ b/exercises/12_options/options2.rs @@ -10,7 +10,7 @@ mod tests { let optional_target = Some(target); // TODO: Make this an if-let statement whose value is `Some`. - word = optional_target { + if let Some(word) = optional_target { assert_eq!(word, target); } } @@ -29,7 +29,7 @@ mod tests { // TODO: Make this a while-let statement. Remember that `Vec::pop()` // adds another layer of `Option`. You can do nested pattern matching // in if-let and while-let statements. - integer = optional_integers.pop() { + while let Some(Some(integer)) = optional_integers.pop() { assert_eq!(integer, cursor); cursor -= 1; } diff --git a/exercises/12_options/options3.rs b/exercises/12_options/options3.rs index 4cedb512..495b3835 100644 --- a/exercises/12_options/options3.rs +++ b/exercises/12_options/options3.rs @@ -9,7 +9,7 @@ fn main() { // TODO: Fix the compiler error by adding something to this match statement. match optional_point { - Some(p) => println!("Co-ordinates are {},{}", p.x, p.y), + Some(ref p) => println!("Co-ordinates are {},{}", p.x, p.y), _ => panic!("No match!"), } diff --git a/exercises/13_error_handling/errors1.rs b/exercises/13_error_handling/errors1.rs index ec7cb3cb..5ec9a6c4 100644 --- a/exercises/13_error_handling/errors1.rs +++ b/exercises/13_error_handling/errors1.rs @@ -4,12 +4,12 @@ // construct to `Option` that can be used to express error conditions. Change // the function signature and body to return `Result` instead // of `Option`. -fn generate_nametag_text(name: String) -> Option { +fn generate_nametag_text(name: String) -> Result { if name.is_empty() { // Empty names aren't allowed. - None + Err("Empty names aren't allowed".to_string()) } else { - Some(format!("Hi! My name is {name}")) + Ok(format!("Hi! My name is {name}")) } } diff --git a/exercises/13_error_handling/errors2.rs b/exercises/13_error_handling/errors2.rs index defe359b..8da62b14 100644 --- a/exercises/13_error_handling/errors2.rs +++ b/exercises/13_error_handling/errors2.rs @@ -22,8 +22,10 @@ fn total_cost(item_quantity: &str) -> Result { // TODO: Handle the error case as described above. let qty = item_quantity.parse::(); - - Ok(qty * cost_per_item + processing_fee) + match qty { + Ok(v) => { Ok(v * cost_per_item + processing_fee) } + Err(e) => { Err(e) } + } } fn main() { diff --git a/exercises/13_error_handling/errors3.rs b/exercises/13_error_handling/errors3.rs index 33a7b877..58b1e16a 100644 --- a/exercises/13_error_handling/errors3.rs +++ b/exercises/13_error_handling/errors3.rs @@ -19,7 +19,7 @@ fn main() { let mut tokens = 100; let pretend_user_input = "8"; - let cost = total_cost(pretend_user_input)?; + let cost = total_cost(pretend_user_input).unwrap(); if cost > tokens { println!("You can't afford that many!"); diff --git a/exercises/13_error_handling/errors4.rs b/exercises/13_error_handling/errors4.rs index e41d5945..cfb2ac6e 100644 --- a/exercises/13_error_handling/errors4.rs +++ b/exercises/13_error_handling/errors4.rs @@ -11,8 +11,13 @@ struct PositiveNonzeroInteger(u64); impl PositiveNonzeroInteger { fn new(value: i64) -> Result { - // TODO: This function shouldn't always return an `Ok`. - Ok(Self(value as u64)) + if value > 0 { + Ok(Self(value as u64)) + } else if value == 0 { + Err(CreationError::Zero) + } else { + Err(CreationError::Negative) + } } } diff --git a/exercises/13_error_handling/errors5.rs b/exercises/13_error_handling/errors5.rs index 57218351..7b9019af 100644 --- a/exercises/13_error_handling/errors5.rs +++ b/exercises/13_error_handling/errors5.rs @@ -48,7 +48,7 @@ impl PositiveNonzeroInteger { // TODO: Add the correct return type `Result<(), Box>`. What can we // use to describe both errors? Is there a trait which both errors implement? -fn main() { +fn main() -> Result<(), Box> { let pretend_user_input = "42"; let x: i64 = pretend_user_input.parse()?; println!("output={:?}", PositiveNonzeroInteger::new(x)?); diff --git a/exercises/13_error_handling/errors6.rs b/exercises/13_error_handling/errors6.rs index b656c617..988afe7e 100644 --- a/exercises/13_error_handling/errors6.rs +++ b/exercises/13_error_handling/errors6.rs @@ -25,7 +25,7 @@ impl ParsePosNonzeroError { } // TODO: Add another error conversion function here. - // fn from_parseint(???) -> Self { ??? } + fn from_parseint(err: ParseIntError) -> Self { Self::ParseInt(err) } } #[derive(PartialEq, Debug)] @@ -43,7 +43,7 @@ impl PositiveNonzeroInteger { fn parse(s: &str) -> Result { // TODO: change this to return an appropriate error instead of panicking // when `parse()` returns an error. - let x: i64 = s.parse().unwrap(); + let x: i64 = s.parse().map_err(ParsePosNonzeroError::from_parseint)?; Self::new(x).map_err(ParsePosNonzeroError::from_creation) } }