From cd34cb5bf5a1fdebe9db1093dc1df422ed256fc3 Mon Sep 17 00:00:00 2001 From: Hariettemaina Date: Mon, 22 May 2023 17:06:54 +0300 Subject: [PATCH] else and if --- exercises/options/options1.rs | 14 +++++++++++--- exercises/options/options2.rs | 5 ++--- exercises/options/options3.rs | 4 ++-- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/exercises/options/options1.rs b/exercises/options/options1.rs index d1735c2f..388ef96f 100644 --- a/exercises/options/options1.rs +++ b/exercises/options/options1.rs @@ -1,7 +1,7 @@ // 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 @@ -10,9 +10,17 @@ 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. - ??? + if time_of_day < 22 { + Some(5) + } else if time_of_day >= 22 && time_of_day <= 24 { + Some(0) + } else { + None + } } + + #[cfg(test)] mod tests { use super::*; @@ -30,6 +38,6 @@ 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); + assert_eq!(icecreams, Some(5)); } } diff --git a/exercises/options/options2.rs b/exercises/options/options2.rs index b1120471..0182f7d0 100644 --- a/exercises/options/options2.rs +++ b/exercises/options/options2.rs @@ -1,7 +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 { @@ -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); } } @@ -28,7 +27,7 @@ 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() { + while let Some(Some(integer)) = optional_integers.pop() { assert_eq!(integer, range); range -= 1; } diff --git a/exercises/options/options3.rs b/exercises/options/options3.rs index 3f995c52..c4c0f5eb 100644 --- a/exercises/options/options3.rs +++ b/exercises/options/options3.rs @@ -1,7 +1,7 @@ // options3.rs // Execute `rustlings hint options3` or use the `hint` watch subcommand for a hint. -// I AM NOT DONE + struct Point { x: i32, @@ -12,7 +12,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.