Reputation: 8467
Let's say I have the enum:
enum Foo {
Bar {baz: Option<Buzz> },
}
struct Buzz {}
is there a way to match on whether baz
is None
or not?
How to match struct fields in Rust? doesn't seem to work because Rust interprets
match foo {
Foo::Bar { baz: Buzz } => {
},
Foo::Bar { baz: None } => {
}
}
the baz: Bar
as a rename.
Upvotes: 0
Views: 102
Reputation: 59817
The opposite of None
is Some
:
let foo = Foo::Bar{ baz: None };
match foo {
Foo::Bar{ baz: Some(_) } => println!("Bar with some"),
Foo::Bar{ baz: None } => println!("Bar with none"),
}
Upvotes: 3