mirror of
https://github.com/notohh/rustlings.git
synced 2024-11-21 21:42:23 -05:00
option2 solution
This commit is contained in:
parent
c31e15c4cf
commit
a91888e79e
3 changed files with 44 additions and 8 deletions
|
@ -9,7 +9,7 @@ mod tests {
|
||||||
let target = "rustlings";
|
let target = "rustlings";
|
||||||
let optional_target = Some(target);
|
let optional_target = Some(target);
|
||||||
|
|
||||||
// TODO: Make this an if let statement whose value is "Some" type
|
// TODO: Make this an if-let statement whose value is `Some`.
|
||||||
word = optional_target {
|
word = optional_target {
|
||||||
assert_eq!(word, target);
|
assert_eq!(word, target);
|
||||||
}
|
}
|
||||||
|
@ -20,15 +20,15 @@ mod tests {
|
||||||
let range = 10;
|
let range = 10;
|
||||||
let mut optional_integers: Vec<Option<i8>> = vec![None];
|
let mut optional_integers: Vec<Option<i8>> = vec![None];
|
||||||
|
|
||||||
for i in 1..(range + 1) {
|
for i in 1..=range {
|
||||||
optional_integers.push(Some(i));
|
optional_integers.push(Some(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut cursor = range;
|
let mut cursor = range;
|
||||||
|
|
||||||
// TODO: make this a while let statement - remember that vector.pop also
|
// TODO: Make this a while-let statement. Remember that `Vec::pop()`
|
||||||
// adds another layer of Option<T>. You can stack `Option<T>`s into
|
// adds another layer of `Option`. You can do nested pattern matching
|
||||||
// while let and if let.
|
// in if-let and while-let statements.
|
||||||
integer = optional_integers.pop() {
|
integer = optional_integers.pop() {
|
||||||
assert_eq!(integer, cursor);
|
assert_eq!(integer, cursor);
|
||||||
cursor -= 1;
|
cursor -= 1;
|
||||||
|
|
|
@ -616,9 +616,9 @@ Check out:
|
||||||
- https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html
|
- https://doc.rust-lang.org/rust-by-example/flow_control/if_let.html
|
||||||
- https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html
|
- https://doc.rust-lang.org/rust-by-example/flow_control/while_let.html
|
||||||
|
|
||||||
Remember that `Option`s can be stacked in `if let` and `while let`.
|
Remember that `Option`s can be nested in if-let and while-let statements.
|
||||||
|
|
||||||
For example: `Some(Some(variable)) = variable2`
|
For example: `if let Some(Some(x)) = y`
|
||||||
|
|
||||||
Also see `Option::flatten`
|
Also see `Option::flatten`
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -1 +1,37 @@
|
||||||
// Solutions will be available before the stable release. Thank you for testing the beta version 🥰
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
#[test]
|
||||||
|
fn simple_option() {
|
||||||
|
let target = "rustlings";
|
||||||
|
let optional_target = Some(target);
|
||||||
|
|
||||||
|
// if-let
|
||||||
|
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;
|
||||||
|
|
||||||
|
// while-let with nested pattern matching
|
||||||
|
while let Some(Some(integer)) = optional_integers.pop() {
|
||||||
|
assert_eq!(integer, cursor);
|
||||||
|
cursor -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
assert_eq!(cursor, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue