2022-07-14 17:34:50 +02:00
|
|
|
// options2.rs
|
2022-07-14 17:53:42 +02:00
|
|
|
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a hint.
|
2020-04-07 20:16:10 +02:00
|
|
|
|
|
|
|
// I AM NOT DONE
|
|
|
|
|
2022-08-17 12:50:34 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn simple_option() {
|
|
|
|
let target = "rustlings";
|
|
|
|
let optional_target = Some(target);
|
2020-04-07 20:16:10 +02:00
|
|
|
|
2022-08-17 12:50:34 +02:00
|
|
|
// TODO: Make this an if let statement whose value is "Some" type
|
2022-08-17 12:54:23 +02:00
|
|
|
word = optional_target {
|
2022-08-17 12:50:34 +02:00
|
|
|
assert_eq!(word, target);
|
|
|
|
}
|
2020-04-07 20:16:10 +02:00
|
|
|
}
|
|
|
|
|
2022-08-17 12:50:34 +02:00
|
|
|
#[test]
|
|
|
|
fn layered_option() {
|
|
|
|
let mut range = 10;
|
|
|
|
let mut optional_integers: Vec<Option<i8>> = Vec::new();
|
|
|
|
for i in 0..(range + 1) {
|
|
|
|
optional_integers.push(Some(i));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: make this a while let statement - remember that vector.pop also adds another layer of Option<T>
|
|
|
|
// You can stack `Option<T>`'s into while let and if let
|
2022-08-17 12:54:23 +02:00
|
|
|
integer = optional_integers.pop() {
|
2022-08-17 12:50:34 +02:00
|
|
|
assert_eq!(integer, range);
|
|
|
|
range -= 1;
|
|
|
|
}
|
2020-04-07 20:16:10 +02:00
|
|
|
}
|
|
|
|
}
|