[Feature Request] Allow multiple `..` per pattern

Hello,
Taking this code as an example:

match [0,1,2,3,4,5] {
     [a@..,2,b@..,4,c@..] => (),
     _ =>()
}

It fails with .. can only be used once per slice pattern.
But I think multiple .. in a pattern can be unambiguous if they are separated by a const value.

I think allowing this would be a nice upgrade for pattern matching.

This isn't the right forum for feature requests, they should go to internals.rust-lang.org or the issue tracker at GitHub - rust-lang/rust: Empowering everyone to build reliable and efficient software..

That said, what should this print?

match [2, 4, 4, 5] {
    [b @ .., 4, c @ ..] => println!("{:?} {:?}", b, c),
    _ => println!("no match"),
}
3 Likes

Same as the current behavior which stops at the first match, you can test with

match [1,2,2,3] {
..:     [a@..,2,2,3] => println!("{:?}",a),
..:     _ => ()
..: }

So in your example it would be b: [2], c: [4,5]

In your example the match is unique: there's only one possible value of a. In my example there are two possible values of (b, c), namely ([2], [4, 5]) and ([2, 4], [5]). I don't think any of the pattern-matching constructs currently in the language permit nonuniqueness like that.

4 Likes

I see, you're right. I guess I think ([2], [4, 5]) is the intuitive behavior. But yes It would need a decision about nonuniqueness so its not as straight forward as I initially thought.

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.