options1.rs (1246B)
1 use core::option::Option::{None, Some}; 2 3 // This function returns how much ice cream there is left in the fridge. 4 // If it's before 22:00 (24-hour system), then 5 scoops are left. At 22:00, 5 // someone eats it all, so no ice cream is left (value 0). Return `None` if 6 // `hour_of_day` is higher than 23. 7 fn maybe_ice_cream(hour_of_day: u16) -> Option<u16> { 8 // TODO: Complete the function body. 9 match hour_of_day { 10 0..=21 => Some(5), 11 22..=23 => Some(0), 12 _ => None, 13 } 14 } 15 16 fn main() { 17 // You can optionally experiment here. 18 } 19 20 #[cfg(test)] 21 mod tests { 22 use super::*; 23 24 #[test] 25 fn raw_value() { 26 // TODO: Fix this test. How do you get the value contained in the 27 // Option? 28 let ice_creams = maybe_ice_cream(12).unwrap(); 29 30 assert_eq!(ice_creams, 5); // Don't change this line. 31 } 32 33 #[test] 34 fn check_ice_cream() { 35 assert_eq!(maybe_ice_cream(0), Some(5)); 36 assert_eq!(maybe_ice_cream(9), Some(5)); 37 assert_eq!(maybe_ice_cream(18), Some(5)); 38 assert_eq!(maybe_ice_cream(22), Some(0)); 39 assert_eq!(maybe_ice_cream(23), Some(0)); 40 assert_eq!(maybe_ice_cream(24), None); 41 assert_eq!(maybe_ice_cream(25), None); 42 } 43 }