Non-exhaustively matching a Struct Variant


I have a somewhat complex Enum, but given this reduced case, do I have an option besides an exhaustive match?

enum E {
    A { <fields> },

let foo = E::B(<obj>);

if let E::B(bar) = foo {
    // do conditional thing on bar
// use foo generically from here forth

Basically, I’m trying to do a single statement if foo is variant B, while doing nothing if its A (or any other of the many variants I might have). Given the format of this enum, this is the only way I know to make this work:

match foo {
     // do the condition thing on bar
     E::B(bar) => { ... },
     // ugly catchall do nothing
     _ => (),
// use foo generically from here forth

Ergonomically it feels like there should be some way to write an if on a variant conditionally, but the if let syntax seems not to work with struct variants according to this error I’m getting:

expected tuple struct/variant, found struct variant

Is there another way to write this, or am I stuck using a match?


That should work – can you provide a more complete code sample? Here’s a playground that compiles and does what you’re trying to do:


If-let should work, if used correctly. Please show a minimized but compilable code example that shows your problem.


The if let you have should work - can you show a runnable example of the issue?


Here is a gist of what triggers the error - I’m trying to match on a struct variant, should have remembered they were different in the OP.

I imagine there is a syntactic way to write that if let statement correctly, but looking through all the reference material isn’t showing me an obvious way.


The pattern needs to use {} (since it’s a struct variant with multiple named fields, you need to name the fields in the pattern) and match on the field names:

You can also ignore fields using something like this:




Yep, that’s it, thank you so much!

I had reread the Rust book versions 1 and 2 and Rust by Example and none of the sections on matching seemed to show an example of this form.


Also, note that the compiler error tells you the correct syntax


I was misconstruing that message to mean to construct a new E::A. I wasn’t even aware you could destructure variant bodies like this.

I was having a “I don’t want curly braces in my if statements!” automatic reaction to seeing it.