diff options
Diffstat (limited to 'exercises/12_options')
| -rw-r--r-- | exercises/12_options/options1.rs | 47 | ||||
| -rw-r--r-- | exercises/12_options/options2.rs | 19 | ||||
| -rw-r--r-- | exercises/12_options/options3.rs | 20 |
3 files changed, 38 insertions, 48 deletions
diff --git a/exercises/12_options/options1.rs b/exercises/12_options/options1.rs index e131b48..9964807 100644 --- a/exercises/12_options/options1.rs +++ b/exercises/12_options/options1.rs @@ -1,19 +1,13 @@ -// 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 -// all, so there'll be no more left :( -fn maybe_icecream(time_of_day: u16) -> Option<u16> { - // We use the 24-hour system here, so 10PM is a value of 22 and 12AM is a - // value of 0 The Option output should gracefully handle cases where - // time_of_day > 23. - // TODO: Complete the function body - remember to return an Option! - ??? +// 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. +} + +fn main() { + // You can optionally experiment here. } #[cfg(test)] @@ -21,19 +15,22 @@ mod tests { use super::*; #[test] + fn raw_value() { + // TODO: Fix this test. How do you get the value contained in the + // Option? + let icecreams = maybe_icecream(12); + + assert_eq!(icecreams, 5); // Don't change this line. + } + + #[test] fn check_icecream() { + assert_eq!(maybe_icecream(0), Some(5)); assert_eq!(maybe_icecream(9), Some(5)); - assert_eq!(maybe_icecream(10), Some(5)); - assert_eq!(maybe_icecream(23), Some(0)); + assert_eq!(maybe_icecream(18), Some(5)); assert_eq!(maybe_icecream(22), Some(0)); + assert_eq!(maybe_icecream(23), Some(0)); + assert_eq!(maybe_icecream(24), None); assert_eq!(maybe_icecream(25), None); } - - #[test] - 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); - } } diff --git a/exercises/12_options/options2.rs b/exercises/12_options/options2.rs index 4d998e7..07c27c6 100644 --- a/exercises/12_options/options2.rs +++ b/exercises/12_options/options2.rs @@ -1,9 +1,6 @@ -// options2.rs -// -// Execute `rustlings hint options2` or use the `hint` watch subcommand for a -// hint. - -// I AM NOT DONE +fn main() { + // You can optionally experiment here. +} #[cfg(test)] mod tests { @@ -12,7 +9,7 @@ mod tests { let target = "rustlings"; let optional_target = Some(target); - // TODO: Make this an if let statement whose value is "Some" type + // TODO: Make this an if-let statement whose value is `Some`. word = optional_target { assert_eq!(word, target); } @@ -23,15 +20,15 @@ mod tests { let range = 10; let mut optional_integers: Vec<Option<i8>> = vec![None]; - for i in 1..(range + 1) { + for i in 1..=range { optional_integers.push(Some(i)); } let mut cursor = range; - // TODO: make this a while let statement - remember that vector.pop also - // adds another layer of Option<T>. You can stack `Option<T>`s into - // while let and if let. + // TODO: Make this a while-let statement. Remember that `Vec::pop()` + // adds another layer of `Option`. You can do nested pattern matching + // in if-let and while-let statements. integer = optional_integers.pop() { assert_eq!(integer, cursor); cursor -= 1; diff --git a/exercises/12_options/options3.rs b/exercises/12_options/options3.rs index 23c15ea..4cedb51 100644 --- a/exercises/12_options/options3.rs +++ b/exercises/12_options/options3.rs @@ -1,21 +1,17 @@ -// options3.rs -// -// Execute `rustlings hint options3` or use the `hint` watch subcommand for a -// hint. - -// I AM NOT DONE - +#[derive(Debug)] struct Point { x: i32, y: i32, } fn main() { - let y: Option<Point> = Some(Point { x: 100, y: 200 }); + let optional_point = Some(Point { x: 100, y: 200 }); - match y { - Some(p) => println!("Co-ordinates are {},{} ", p.x, p.y), - _ => panic!("no match!"), + // TODO: Fix the compiler error by adding something to this match statement. + match optional_point { + Some(p) => println!("Co-ordinates are {},{}", p.x, p.y), + _ => panic!("No match!"), } - y; // Fix without deleting this line. + + println!("{optional_point:?}"); // Don't change this line. } |
