Ignoring variants when matchin enums

In the rust book we have the section Ignoring Variants.

It uses .. to ignore the value, while I have only seen _ to do that so far, so is there any semantic difference in matching x and y in the following code example?

enum OptInt {
    Value(i32),
    Missing,
}

fn main() {
    let x = OptInt::Value(5);
    let y = OptInt::Value(5);

    match x {
        OptInt::Value(..) => println!("Got an int!"),
        OptInt::Missing => println!("No such luck."),
    }

    match y {
        OptInt::Value(_) => println!("Got an int!"),
        OptInt::Missing => println!("No such luck."),
    }
}

You can use _ as a placeholder for exactly one field, but .. can elide an arbitrary number of fields. For example, if your enum had Pair(i32, i32), you could match Pair(_, _) or Pair(..).

3 Likes

Oh, this is nice, perhaps this should be made more clear in the book…

Check the Second Edition of the Rust Book.
It’s better for learning the language, but explore less deeply the exoteric potentials of Rust.

https://doc.rust-lang.org/book/second-edition/ch18-03-pattern-syntax.html

The book even have a good chapter order, not like the first edition who is a mess of unordered notions

1 Like

Please note that the book linked in the OP is

also for Rust 1.2.

I landed via Google on that page, at first I thought it were the second edition of the book.

I’ll take a closer look at the edition in the future :slight_smile:

The address bar save the life sometimes with reference by Google!