ampron
ampron

Reputation: 3666

How to ignore a member of a struct-like enum variant in pattern matching?

How do I remove the unused_variables warning from the following code?

pub enum Foo {
    Bar {
        a: i32,
        b: i32,
        c: i32,
    },
    Baz,
}

fn main() {
    let myfoo = Foo::Bar { a: 1, b: 2, c: 3 };
    let x: i32 = match myfoo {
        Foo::Bar { a, b, c } => b * b,
        Foo::Baz => -1,
    };
    assert_eq!(x, 4);
}

I know I can ignore struct members after a certain point with:

Foo::Bar { a, .. } => // do stuff with 'a'

But I can't find documentation anywhere that explains how to ignore individual struct members.

Code on Rust Playground

Upvotes: 12

Views: 5954

Answers (1)

Shepmaster
Shepmaster

Reputation: 432039

I know I can ignore struct members after a certain point with:

The .. is not positional. It just means "all the other fields":

Foo::Bar { b, .. } => b * b,

Upvotes: 22

Related Questions