0

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.

Foobar
  • 7,458
  • 16
  • 81
  • 161
  • 1
    Does this answer your question? [How to match struct fields in Rust?](https://stackoverflow.com/questions/41390457/how-to-match-struct-fields-in-rust) – kmdreko Apr 18 '21 at 03:20
  • Oh, the solution there doesn't seem to work. I'll add an edit. – Foobar Apr 18 '21 at 03:22

1 Answers1

3

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"),
}
kmdreko
  • 42,554
  • 6
  • 57
  • 106