summaryrefslogtreecommitdiff
path: root/rustlings/exercises/12_options
diff options
context:
space:
mode:
Diffstat (limited to 'rustlings/exercises/12_options')
-rw-r--r--rustlings/exercises/12_options/README.md21
-rw-r--r--rustlings/exercises/12_options/options1.rs36
-rw-r--r--rustlings/exercises/12_options/options2.rs39
-rw-r--r--rustlings/exercises/12_options/options3.rs17
4 files changed, 113 insertions, 0 deletions
diff --git a/rustlings/exercises/12_options/README.md b/rustlings/exercises/12_options/README.md
new file mode 100644
index 0000000..624572a
--- /dev/null
+++ b/rustlings/exercises/12_options/README.md
@@ -0,0 +1,21 @@
+# Options
+
+Type Option represents an optional value: every Option is either Some and contains a value, or None, and does not.
+Option types are very common in Rust code, as they have a number of uses:
+
+- Initial values
+- Return values for functions that are not defined over their entire input range (partial functions)
+- Return value for otherwise reporting simple errors, where None is returned on error
+- Optional struct fields
+- Struct fields that can be loaned or "taken"
+- Optional function arguments
+- Nullable pointers
+- Swapping things out of difficult situations
+
+## Further Information
+
+- [Option Enum Format](https://doc.rust-lang.org/book/ch10-01-syntax.html#in-enum-definitions)
+- [Option Module Documentation](https://doc.rust-lang.org/std/option/)
+- [Option Enum Documentation](https://doc.rust-lang.org/std/option/enum.Option.html)
+- [if let](https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html)
+- [while let](https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html)
diff --git a/rustlings/exercises/12_options/options1.rs b/rustlings/exercises/12_options/options1.rs
new file mode 100644
index 0000000..d0c412a
--- /dev/null
+++ b/rustlings/exercises/12_options/options1.rs
@@ -0,0 +1,36 @@
+// This function returns how much ice cream 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 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<u16> {
+ // TODO: Complete the function body.
+}
+
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+
+ #[test]
+ fn raw_value() {
+ // TODO: Fix this test. How do you get the value contained in the
+ // Option?
+ let ice_creams = maybe_ice_cream(12);
+
+ assert_eq!(ice_creams, 5); // Don't change this line.
+ }
+
+ #[test]
+ fn check_ice_cream() {
+ assert_eq!(maybe_ice_cream(0), Some(5));
+ assert_eq!(maybe_ice_cream(9), Some(5));
+ assert_eq!(maybe_ice_cream(18), Some(5));
+ assert_eq!(maybe_ice_cream(22), Some(0));
+ assert_eq!(maybe_ice_cream(23), Some(0));
+ assert_eq!(maybe_ice_cream(24), None);
+ assert_eq!(maybe_ice_cream(25), None);
+ }
+}
diff --git a/rustlings/exercises/12_options/options2.rs b/rustlings/exercises/12_options/options2.rs
new file mode 100644
index 0000000..07c27c6
--- /dev/null
+++ b/rustlings/exercises/12_options/options2.rs
@@ -0,0 +1,39 @@
+fn main() {
+ // You can optionally experiment here.
+}
+
+#[cfg(test)]
+mod tests {
+ #[test]
+ fn simple_option() {
+ let target = "rustlings";
+ let optional_target = Some(target);
+
+ // TODO: Make this an if-let statement whose value is `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;
+
+ // 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;
+ }
+
+ assert_eq!(cursor, 0);
+ }
+}
diff --git a/rustlings/exercises/12_options/options3.rs b/rustlings/exercises/12_options/options3.rs
new file mode 100644
index 0000000..c97b1d3
--- /dev/null
+++ b/rustlings/exercises/12_options/options3.rs
@@ -0,0 +1,17 @@
+#[derive(Debug)]
+struct Point {
+ x: i32,
+ y: i32,
+}
+
+fn main() {
+ let optional_point = Some(Point { x: 100, y: 200 });
+
+ // TODO: Fix the compiler error by adding something to this match statement.
+ match optional_point {
+ Some(p) => println!("Coordinates are {},{}", p.x, p.y),
+ _ => panic!("No match!"),
+ }
+
+ println!("{optional_point:?}"); // Don't change this line.
+}