Does matches! macro support enum decouple?

See playground

It seems that the enum decouple didn't take affect in matches! macro

if matches!(color, E::Color(a1, a2, a3)) {
    println!("{}, {}, {}", a1, a2, a3);
}

For this short example, I need to write this code instead

    match color {
        E::Color(a1, a2, a3) => {
            println!("{}, {}, {}", a1, a2, a3);
        }
        _ => {}
    }
    if let E::Color(a1, a2, a3) = color {
        println!("{}, {}, {}", a1, a2, a3);
    }

matches! is only for returning a bool result, not for using variables in the pattern match later.

9 Likes

Yes, indeed. But this is very useful in this scenario. This is so convenient that I'm going to write it like this.

But I can't find your answear from the document

That is true, the doc for matches! only tells you what it does, not what it does not do:

Returns whether the given expression matches the provided pattern.

You can find more information on if let ... here in the book.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.