options1.rs (1075B)
1 // This function returns how much icecream there is left in the fridge. 2 // If it's before 22:00 (24-hour system), then 5 scoops are left. At 22:00, 3 // someone eats it all, so no icecream is left (value 0). Return `None` if 4 // `hour_of_day` is higher than 23. 5 fn maybe_icecream(hour_of_day: u16) -> Option<u16> { 6 match hour_of_day { 7 0..=21 => Some(5), 8 22..=23 => Some(0), 9 _ => None, 10 } 11 } 12 13 fn main() { 14 // You can optionally experiment here. 15 } 16 17 #[cfg(test)] 18 mod tests { 19 use super::*; 20 21 #[test] 22 fn raw_value() { 23 // Using `unwrap` is fine in a test. 24 let icecreams = maybe_icecream(12).unwrap(); 25 26 assert_eq!(icecreams, 5); 27 } 28 29 #[test] 30 fn check_icecream() { 31 assert_eq!(maybe_icecream(0), Some(5)); 32 assert_eq!(maybe_icecream(9), Some(5)); 33 assert_eq!(maybe_icecream(18), Some(5)); 34 assert_eq!(maybe_icecream(22), Some(0)); 35 assert_eq!(maybe_icecream(23), Some(0)); 36 assert_eq!(maybe_icecream(24), None); 37 assert_eq!(maybe_icecream(25), None); 38 } 39 }