rustlings
rustlings copied to clipboard
options1.rs maybe_icecream undefined for hour_of_day == 23
https://github.com/rust-lang/rustlings/blob/main/exercises/12_options/options1.rs
options1.rs maybe_icecream undefined for hour_of_day == 23
// This function returns how much icecream there is left in the fridge.
// If it's before 22:00 (24-hour system), then 5 scoops are left. At 22:00,
// 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<u16> {
// TODO: Complete the function body.
if hour_of_day < 22 {
Some(5) // If it's before 22:00 (24-hour system), then 5 scoops are left.
} else if hour_of_day == 22 {
Some(0) // At 22:00, someone eats it all, so no icecream is left (value 0)
} else if hour_of_day > 23 {
None // Return `None` if `hour_of_day` is higher than 23.
} else {
Some(0) // this is only here to satisfy the contradicting test case
// assert_eq!(maybe_icecream(23), Some(0));
}
}
If we should follow the test cases, then I suggest changing the line
//
hour_of_dayis higher than 23.
to something like
//
hour_of_dayis 23 or higher
I think the problem is that you dont include 23 in second condition else if hour_of_day == 22
In exercise comments we see:
At 22:00, someone eats it all, so no icecream is left (value 0)
That means we still have value 0 at 22:15, 22:30 etc including 23:00
But just AFTER 23:00 (>) we need to return None
My solution:
fn maybe_icecream(hour_of_day: u16) -> Option<u16> {
match hour_of_day {
0..=21 => Some(5),
22..=23 => Some(0),
_ => None,
}
}