Initial commit
This commit is contained in:
21
exercises/12_options/README.md
Normal file
21
exercises/12_options/README.md
Normal file
@@ -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)
|
||||
45
exercises/12_options/options1.rs
Normal file
45
exercises/12_options/options1.rs
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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> {
|
||||
// TODO: Complete the function body.
|
||||
let result: Option<u16>;
|
||||
if hour_of_day < 22 {
|
||||
result = Some(5);
|
||||
} else if (22..=23).contains(&hour_of_day) {
|
||||
result = Some(0)
|
||||
} else {
|
||||
result = None
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
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 icecreams = maybe_icecream(12).unwrap();
|
||||
|
||||
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(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);
|
||||
}
|
||||
}
|
||||
39
exercises/12_options/options2.rs
Normal file
39
exercises/12_options/options2.rs
Normal file
@@ -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`.
|
||||
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;
|
||||
|
||||
// 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.
|
||||
while let Some(Some(integer)) = optional_integers.pop() {
|
||||
assert_eq!(integer, cursor);
|
||||
cursor -= 1;
|
||||
}
|
||||
|
||||
assert_eq!(cursor, 0);
|
||||
}
|
||||
}
|
||||
17
exercises/12_options/options3.rs
Normal file
17
exercises/12_options/options3.rs
Normal file
@@ -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(ref p) => println!("Co-ordinates are {},{}", p.x, p.y),
|
||||
_ => panic!("No match!"),
|
||||
}
|
||||
|
||||
println!("{optional_point:?}"); // Don't change this line.
|
||||
}
|
||||
Reference in New Issue
Block a user