Is there a way of doing the following with if-let syntax?
let x: Option<u32> = None;
let y: Option<u32> = Some(13);
if let Some(x) = x || y {
// ... where x = Some(13)
}
Is there a way of doing the following with if-let syntax?
let x: Option<u32> = None;
let y: Option<u32> = Some(13);
if let Some(x) = x || y {
// ... where x = Some(13)
}
I'm not sure I understand what you want to achieve. Do you want to enter the branch if either x
or y
is Some(13)
? In which case you can just use Option::or
:
fn main() {
let x: Option<u32> = None;
let y: Option<u32> = Some(13);
if let Some(13) = x.or(y) {
println!("yes");
}
}
If you want to bind the inner value of y
if y
is Some(_)
to a variable if x
is Some(13)
, you could pattern-match a tuple:
fn main() {
let x: Option<u32> = Some(13);
let y: Option<u32> = Some(42);
if let (Some(13), Some(y_inner)) = (x, y) {
println!("yes: {y_inner}");
}
}
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.