Skip to content

Catch Javascript style "rest patterns" #120591

Closed
@camsteffen

Description

@camsteffen

Code

fn test() {
    match [] {
        [] => {}
        [head, ..tail] => {}
    }
}

Current output

error[E0425]: cannot find value `tail` in this scope
 --> src/lib.rs:4:18
  |
4 |         [head, ..tail] => {}
  |                  ^^^^ not found in this scope

error[E0658]: exclusive range pattern syntax is experimental
 --> src/lib.rs:4:16
  |
4 |         [head, ..tail] => {}
  |                ^^^^^^
  |
  = note: see issue #37854 <https://github.com/rust-lang/rust/issues/37854> for more information

error[E0527]: pattern requires 2 elements but array has 0
 --> src/lib.rs:4:9
  |
4 |         [head, ..tail] => {}
  |         ^^^^^^^^^^^^^^ expected 0 elements

Some errors have detailed explanations: E0425, E0527, E0658.
For more information about an error, try `rustc --explain E0425`.

Desired output

Did you mean to bind `tail` to the rest of the elements?

    [head, tail @ ..] => {}

Rationale and extra context

No response

Other cases

It could work with either two or three dots.

Rust Version

rustc 1.77.0-nightly (89e2160c4 2023-12-27)
binary: rustc
commit-hash: 89e2160c4ca5808657ed55392620ed1dbbce78d1
commit-date: 2023-12-27
host: x86_64-apple-darwin
release: 1.77.0-nightly
LLVM version: 17.0.6

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-resolveArea: Name/path resolution done by `rustc_resolve` specificallyT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions