From 1da0f56d76e5d5117eac05af092bf0ab58b14c8f Mon Sep 17 00:00:00 2001 From: Hunter Kvalevog Date: Sun, 27 Sep 2026 11:17:50 -0500 Subject: rustlings: complete 12 --- rustlings/.rustlings-state.txt | 7 ++++-- rustlings/exercises/12_options/options1.rs | 10 ++++++-- rustlings/exercises/12_options/options2.rs | 4 +-- rustlings/exercises/12_options/options3.rs | 2 +- rustlings/solutions/12_options/options1.rs | 39 ++++++++++++++++++++++++++++-- rustlings/solutions/12_options/options2.rs | 37 ++++++++++++++++++++++++++-- rustlings/solutions/12_options/options3.rs | 27 +++++++++++++++++++-- 7 files changed, 113 insertions(+), 13 deletions(-) diff --git a/rustlings/.rustlings-state.txt b/rustlings/.rustlings-state.txt index 8b4959e..dac5f44 100644 --- a/rustlings/.rustlings-state.txt +++ b/rustlings/.rustlings-state.txt @@ -1,6 +1,6 @@ DON'T EDIT THIS FILE! -options1 +errors1 intro1 intro2 @@ -48,4 +48,7 @@ modules3 hashmaps1 hashmaps2 hashmaps3 -quiz2 \ No newline at end of file +quiz2 +options1 +options2 +options3 \ No newline at end of file diff --git a/rustlings/exercises/12_options/options1.rs b/rustlings/exercises/12_options/options1.rs index d0c412a..be97952 100644 --- a/rustlings/exercises/12_options/options1.rs +++ b/rustlings/exercises/12_options/options1.rs @@ -3,7 +3,13 @@ // someone eats it all, so no ice cream is left (value 0). Return `None` if // `hour_of_day` is higher than 23. fn maybe_ice_cream(hour_of_day: u16) -> Option { - // TODO: Complete the function body. + if hour_of_day >= 24 { + return None; + } + if hour_of_day >= 22 { + return Some(0); + } + Some(5) } fn main() { @@ -18,7 +24,7 @@ mod tests { fn raw_value() { // TODO: Fix this test. How do you get the value contained in the // Option? - let ice_creams = maybe_ice_cream(12); + let ice_creams = maybe_ice_cream(12).unwrap(); assert_eq!(ice_creams, 5); // Don't change this line. } diff --git a/rustlings/exercises/12_options/options2.rs b/rustlings/exercises/12_options/options2.rs index 07c27c6..1039d61 100644 --- a/rustlings/exercises/12_options/options2.rs +++ b/rustlings/exercises/12_options/options2.rs @@ -10,7 +10,7 @@ mod tests { let optional_target = Some(target); // TODO: Make this an if-let statement whose value is `Some`. - word = optional_target { + if let Some(word) = optional_target { assert_eq!(word, target); } } @@ -29,7 +29,7 @@ mod tests { // 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() { + while let Some(Some(integer)) = optional_integers.pop() { assert_eq!(integer, cursor); cursor -= 1; } diff --git a/rustlings/exercises/12_options/options3.rs b/rustlings/exercises/12_options/options3.rs index c97b1d3..aecb7a5 100644 --- a/rustlings/exercises/12_options/options3.rs +++ b/rustlings/exercises/12_options/options3.rs @@ -9,7 +9,7 @@ fn main() { // TODO: Fix the compiler error by adding something to this match statement. match optional_point { - Some(p) => println!("Coordinates are {},{}", p.x, p.y), + Some(ref p) => println!("Coordinates are {},{}", p.x, p.y), _ => panic!("No match!"), } diff --git a/rustlings/solutions/12_options/options1.rs b/rustlings/solutions/12_options/options1.rs index dcf2377..4d615dd 100644 --- a/rustlings/solutions/12_options/options1.rs +++ b/rustlings/solutions/12_options/options1.rs @@ -1,4 +1,39 @@ +// 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 { + match hour_of_day { + 0..=21 => Some(5), + 22..=23 => Some(0), + _ => None, + } +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn raw_value() { + // Using `unwrap` is fine in a test. + let icecreams = maybe_icecream(12).unwrap(); + + assert_eq!(icecreams, 5); + } + + #[test] + fn check_icecream() { + assert_eq!(maybe_icecream(0), Some(5)); + assert_eq!(maybe_icecream(9), Some(5)); + 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); + } } diff --git a/rustlings/solutions/12_options/options2.rs b/rustlings/solutions/12_options/options2.rs index dcf2377..0f24665 100644 --- a/rustlings/solutions/12_options/options2.rs +++ b/rustlings/solutions/12_options/options2.rs @@ -1,4 +1,37 @@ fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + // You can optionally experiment here. +} + +#[cfg(test)] +mod tests { + #[test] + fn simple_option() { + let target = "rustlings"; + let optional_target = Some(target); + + // if-let + if let Some(word) = optional_target { + assert_eq!(word, target); + } + } + + #[test] + fn layered_option() { + let range = 10; + let mut optional_integers: Vec> = vec![None]; + + for i in 1..=range { + optional_integers.push(Some(i)); + } + + let mut cursor = range; + + // while-let with nested pattern matching + while let Some(Some(integer)) = optional_integers.pop() { + assert_eq!(integer, cursor); + cursor -= 1; + } + + assert_eq!(cursor, 0); + } } diff --git a/rustlings/solutions/12_options/options3.rs b/rustlings/solutions/12_options/options3.rs index dcf2377..c918f71 100644 --- a/rustlings/solutions/12_options/options3.rs +++ b/rustlings/solutions/12_options/options3.rs @@ -1,4 +1,27 @@ +#[derive(Debug)] +struct Point { + x: i32, + y: i32, +} + fn main() { - // DON'T EDIT THIS SOLUTION FILE! - // It will be automatically filled after you finish the exercise. + let optional_point = Some(Point { x: 100, y: 200 }); + + // Solution 1: Matching over the `Option` (not `&Option`) but without moving + // out of the `Some` variant. + match optional_point { + Some(ref p) => println!("Coordinates are {},{}", p.x, p.y), + // ^^^ added + _ => panic!("No match!"), + } + + // Solution 2: Matching over a reference (`&Option`) by added `&` before + // `optional_point`. + match &optional_point { + //^ added + Some(p) => println!("Coordinates are {},{}", p.x, p.y), + _ => panic!("No match!"), + } + + println!("{optional_point:?}"); } -- cgit v1.2.3