2024-04-17 16:46:21 -04:00
|
|
|
fn main() {
|
|
|
|
// You can optionally experiment here.
|
|
|
|
}
|
|
|
|
|
2022-08-17 06:50:34 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn simple_option() {
|
|
|
|
let target = "rustlings";
|
|
|
|
let optional_target = Some(target);
|
2020-04-07 14:16:10 -04:00
|
|
|
|
2024-06-26 08:35:05 -04:00
|
|
|
// TODO: Make this an if-let statement whose value is `Some`.
|
2022-08-17 06:54:23 -04:00
|
|
|
word = optional_target {
|
2022-08-17 06:50:34 -04:00
|
|
|
assert_eq!(word, target);
|
|
|
|
}
|
2020-04-07 14:16:10 -04:00
|
|
|
}
|
|
|
|
|
2022-08-17 06:50:34 -04:00
|
|
|
#[test]
|
|
|
|
fn layered_option() {
|
2023-04-21 01:05:25 -04:00
|
|
|
let range = 10;
|
|
|
|
let mut optional_integers: Vec<Option<i8>> = vec![None];
|
|
|
|
|
2024-06-26 08:35:05 -04:00
|
|
|
for i in 1..=range {
|
2022-08-17 06:50:34 -04:00
|
|
|
optional_integers.push(Some(i));
|
|
|
|
}
|
|
|
|
|
2023-04-21 01:05:25 -04:00
|
|
|
let mut cursor = range;
|
|
|
|
|
2024-06-26 08:35:05 -04:00
|
|
|
// 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.
|
2022-08-17 06:54:23 -04:00
|
|
|
integer = optional_integers.pop() {
|
2023-04-21 01:05:25 -04:00
|
|
|
assert_eq!(integer, cursor);
|
|
|
|
cursor -= 1;
|
2022-08-17 06:50:34 -04:00
|
|
|
}
|
2023-04-21 01:05:25 -04:00
|
|
|
|
|
|
|
assert_eq!(cursor, 0);
|
2020-04-07 14:16:10 -04:00
|
|
|
}
|
|
|
|
}
|