summaryrefslogtreecommitdiff
path: root/rustlings/solutions/12_options
diff options
context:
space:
mode:
authorHunter Kvalevog <hunter@kvog.sh>2026-10-02 21:53:51 -0500
committerHunter Kvalevog <hunter@kvog.sh>2026-10-02 21:53:51 -0500
commit4dfc11d74ab02ac5765ae5d08fb750c91f4744b4 (patch)
tree17ba62056d6b691dd0217b0e8f1b3a5826dce0f6 /rustlings/solutions/12_options
parent02d8f1c62896576e1266553a56980eddbc2a88d7 (diff)
rustlings: move to rust/
Diffstat (limited to 'rustlings/solutions/12_options')
-rw-r--r--rustlings/solutions/12_options/options1.rs39
-rw-r--r--rustlings/solutions/12_options/options2.rs37
-rw-r--r--rustlings/solutions/12_options/options3.rs27
3 files changed, 0 insertions, 103 deletions
diff --git a/rustlings/solutions/12_options/options1.rs b/rustlings/solutions/12_options/options1.rs
deleted file mode 100644
index 4d615dd..0000000
--- a/rustlings/solutions/12_options/options1.rs
+++ /dev/null
@@ -1,39 +0,0 @@
-// 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> {
- match hour_of_day {
- 0..=21 => Some(5),
- 22..=23 => Some(0),
- _ => None,
- }
-}
-
-fn main() {
- // 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
deleted file mode 100644
index 0f24665..0000000
--- a/rustlings/solutions/12_options/options2.rs
+++ /dev/null
@@ -1,37 +0,0 @@
-fn main() {
- // 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<Option<i8>> = 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
deleted file mode 100644
index c918f71..0000000
--- a/rustlings/solutions/12_options/options3.rs
+++ /dev/null
@@ -1,27 +0,0 @@
-#[derive(Debug)]
-struct Point {
- x: i32,
- y: i32,
-}
-
-fn main() {
- 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:?}");
-}