From 4f31cb6dafcea5822a4f1beab29a21d3719ab8b3 Mon Sep 17 00:00:00 2001 From: "David E. Perez Negron R." Date: Tue, 21 Nov 2023 20:00:03 -0600 Subject: [PATCH] Adding options --- exercises/options/options1.rs | 9 +++++---- exercises/options/options2.rs | 11 ++++++----- exercises/options/options3.rs | 3 +-- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index e131b48..7551544 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint options1` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE // This function returns how much icecream there is left in the fridge. // If it's before 10PM, there's 5 pieces left. At 10PM, someone eats them @@ -13,7 +12,9 @@ fn maybe_icecream(time_of_day: u16) -> Option { // value of 0 The Option output should gracefully handle cases where // time_of_day > 23. // TODO: Complete the function body - remember to return an Option! - ??? + if time_of_day < 22 { Some(5) } + else if time_of_day >= 22 && time_of_day < 25 { Some(0) } + else { None } } #[cfg(test)] @@ -33,7 +34,7 @@ mod tests { fn raw_value() { // TODO: Fix this test. How do you get at the value contained in the // Option? - let icecreams = maybe_icecream(12); - assert_eq!(icecreams, 5); + let icecreams = maybe_icecream(12).expect("No icecream"); + assert_eq!(icecreams, 5u16); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index 4d998e7..dc0b61e 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint options2` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE #[cfg(test)] mod tests { @@ -13,7 +12,7 @@ mod tests { let optional_target = Some(target); // TODO: Make this an if let statement whose value is "Some" type - word = optional_target { + if let Some(word) = optional_target { assert_eq!(word, target); } } @@ -32,9 +31,11 @@ mod tests { // TODO: make this a while let statement - remember that vector.pop also // adds another layer of Option. You can stack `Option`s into // while let and if let. - integer = optional_integers.pop() { - assert_eq!(integer, cursor); - cursor -= 1; + while let Some(int) = optional_integers.pop() { + if let Some(integer) = int { + assert_eq!(integer, cursor); + cursor -= 1; + } } assert_eq!(cursor, 0); diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs index 23c15ea..6edce3f 100644 --- a/exercises/options/options3.rs +++ b/exercises/options/options3.rs @@ -3,7 +3,6 @@ // Execute `rustlings hint options3` or use the `hint` watch subcommand for a // hint. -// I AM NOT DONE struct Point { x: i32, @@ -13,7 +12,7 @@ struct Point { fn main() { let y: Option = Some(Point { x: 100, y: 200 }); - match y { + match &y { Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => panic!("no match!"), }