From c9b03ee8f3425e0da021ba873d1f6c01b7f65737 Mon Sep 17 00:00:00 2001 From: Karan Kadam Date: Fri, 23 Dec 2022 13:07:42 +1030 Subject: [PATCH] =?UTF-8?q?feat:=20=F0=9F=8E=B8=20added=20solutions=20for?= =?UTF-8?q?=20exercises=20upto=20#49?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- exercises/options/options1.rs | 10 ++++++---- exercises/options/options2.rs | 8 +++----- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index d1735c2f..c60eed4f 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -1,8 +1,6 @@ // options1.rs // 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 // all, so there'll be no more left :( @@ -10,7 +8,11 @@ fn maybe_icecream(time_of_day: u16) -> Option { // We use the 24-hour system here, so 10PM is a value of 22 // The Option output should gracefully handle cases where time_of_day > 24. - ??? + match time_of_day { + 0..=21 => return Some(5), + 22..=23 => return Some(0), + _ => return None, + } } #[cfg(test)] @@ -29,7 +31,7 @@ mod tests { #[test] fn raw_value() { // TODO: Fix this test. How do you get at the value contained in the Option? - let icecreams = maybe_icecream(12); + let icecreams = maybe_icecream(12).unwrap_or(0); assert_eq!(icecreams, 5); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index b1120471..138507e5 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -1,8 +1,6 @@ // options2.rs // Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - #[cfg(test)] mod tests { use super::*; @@ -13,7 +11,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); } } @@ -28,8 +26,8 @@ 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, range); + while let Some(integer) = optional_integers.pop() { + assert_eq!(integer.unwrap(), range); range -= 1; } }