Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

options1.rs maybe_icecream undefined for hour_of_day == 23 #2206

Open
supervaka opened this issue Feb 15, 2025 · 2 comments
Open

options1.rs maybe_icecream undefined for hour_of_day == 23 #2206

supervaka opened this issue Feb 15, 2025 · 2 comments

Comments

@supervaka
Copy link

supervaka commented Feb 15, 2025

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));
    }
}
@supervaka
Copy link
Author

supervaka commented Feb 15, 2025

If we should follow the test cases, then I suggest changing the line

// hour_of_day is higher than 23.

to something like

// hour_of_day is 23 or higher

@temanmd
Copy link

temanmd commented Feb 19, 2025

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,
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants