2022-07-14 18:34:50 +03:00
|
|
|
// options2.rs
|
2023-05-29 20:39:08 +03:00
|
|
|
//
|
|
|
|
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a
|
|
|
|
// hint.
|
2020-04-07 21:16:10 +03:00
|
|
|
|
|
|
|
// I AM NOT DONE
|
|
|
|
|
2022-08-17 13:50:34 +03:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
#[test]
|
|
|
|
fn simple_option() {
|
|
|
|
let target = "rustlings";
|
|
|
|
let optional_target = Some(target);
|
2020-04-07 21:16:10 +03:00
|
|
|
|
2022-08-17 13:50:34 +03:00
|
|
|
// TODO: Make this an if let statement whose value is "Some" type
|
2022-08-17 13:54:23 +03:00
|
|
|
word = optional_target {
|
2022-08-17 13:50:34 +03:00
|
|
|
assert_eq!(word, target);
|
|
|
|
}
|
2020-04-07 21:16:10 +03:00
|
|
|
}
|
|
|
|
|
2022-08-17 13:50:34 +03:00
|
|
|
#[test]
|
|
|
|
fn layered_option() {
|
2023-04-21 08:05:25 +03:00
|
|
|
let range = 10;
|
|
|
|
let mut optional_integers: Vec<Option<i8>> = vec![None];
|
|
|
|
|
|
|
|
for i in 1..(range + 1) {
|
2022-08-17 13:50:34 +03:00
|
|
|
optional_integers.push(Some(i));
|
|
|
|
}
|
|
|
|
|
2023-04-21 08:05:25 +03:00
|
|
|
let mut cursor = range;
|
|
|
|
|
2023-05-29 20:39:08 +03:00
|
|
|
// 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 13:54:23 +03:00
|
|
|
integer = optional_integers.pop() {
|
2023-04-21 08:05:25 +03:00
|
|
|
assert_eq!(integer, cursor);
|
|
|
|
cursor -= 1;
|
2022-08-17 13:50:34 +03:00
|
|
|
}
|
2023-04-21 08:05:25 +03:00
|
|
|
|
|
|
|
assert_eq!(cursor, 0);
|
2020-04-07 21:16:10 +03:00
|
|
|
}
|
|
|
|
}
|