'Using while let with two variables simultaneously
I'm learning Rust and have been going through leetcode problems. One of them includes merging two linked lists, whose nodes are optional. I want to write a while loop that would go on until at least 1 node becomes None, and I was trying to use the while let loop for that.
However, it looks like the while let syntax supports only one optional, e.g.:
while let Some(n) = node {
// do stuff
}
but I can't write
while let Some(n1) = node1 && Some(n2) = node2 {
}
Am I misunderstanding the syntax? I know I can rewrite it with a while true loop, but is there a more elegant way of doing it?
Also, can one do multiple checks with if let? Like if let None=node1 && None=node2 {return}
Solution 1:[1]
Solution 2:[2]
In addition to what @Netwave said, on nightly you can use the unstable let_chains feature:
#![feature(let_chains)]
while let Some(n1) = node1 && let Some(n2) = node2 {
// ...
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Netwave |
| Solution 2 | Chayim Friedman |
