Refactor options2.rs: Use if let and while let statements

This commit is contained in:
Rock070 2023-12-31 03:22:56 +08:00
parent 439778465d
commit 9e0f7510d9

View File

@ -3,8 +3,6 @@
// Execute `rustlings hint options2` or use the `hint` watch subcommand for a // Execute `rustlings hint options2` or use the `hint` watch subcommand for a
// hint. // hint.
// I AM NOT DONE
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
#[test] #[test]
@ -13,9 +11,9 @@ mod tests {
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" type
word = optional_target { let word = if let Some(target) = optional_target {
assert_eq!(word, target); assert_eq!(word, target);
} };
} }
#[test] #[test]
@ -32,10 +30,10 @@ mod tests {
// TODO: make this a while let statement - remember that vector.pop also // 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 // adds another layer of Option<T>. You can stack `Option<T>`s into
// while let and if let. // while let and if let.
integer = optional_integers.pop() { integer = if let Some(cursor) = optional_integers.pop() {
assert_eq!(integer, cursor); assert_eq!(integer, cursor);
cursor -= 1; cursor -= 1
} };
assert_eq!(cursor, 0); assert_eq!(cursor, 0);
} }