From ca8416cb0ebf0a6cb3481d73f68c917c97267eec Mon Sep 17 00:00:00 2001 From: Phan Huy Hoang Date: Sat, 7 Jan 2023 00:21:37 +0700 Subject: [PATCH] [Add] exercises/options --- exercises/options/options1.rs | 14 ++++++-------- exercises/options/options2.rs | 11 +++-------- exercises/options/options3.rs | 4 +--- 3 files changed, 10 insertions(+), 19 deletions(-) diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index 1f891b0e..123710d3 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -1,16 +1,15 @@ // 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 :( fn maybe_icecream(time_of_day: u16) -> Option { - // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a 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! - ??? + match time_of_day { + 0..=21 => Some(5), + 22..=23 => Some(0), + _ => None + } } #[cfg(test)] @@ -28,8 +27,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(); assert_eq!(icecreams, 5); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index b1120471..55f8ca8c 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::*; @@ -12,8 +10,7 @@ mod tests { let target = "rustlings"; 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); } } @@ -26,10 +23,8 @@ mod tests { optional_integers.push(Some(i)); } - // 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, Some(range)); range -= 1; } } diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs index 3f995c52..1dd0b374 100644 --- a/exercises/options/options3.rs +++ b/exercises/options/options3.rs @@ -1,8 +1,6 @@ // options3.rs // Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE - struct Point { x: i32, y: i32, @@ -12,7 +10,7 @@ fn main() { let y: Option = Some(Point { x: 100, y: 200 }); match y { - Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), + Some(ref p) => println!("Co-ordinates are {},{} ", p.x, p.y), _ => println!("no match"), } y; // Fix without deleting this line.