Why can the following code compile and run successfully, but the VS Code Rust Analyzer extension reports an error?

I am a Rust beginner. here is the complete code:

fn main() {
    let (x, y);
    (x, ..) = (3, 4);
    [.., y] = [1, 2];

    // Fill in the blank to make the code work
    assert_eq!([x, y], [3, 2]);
}

Why does

[.., y] = [1, 2];
error: pattern requires 1 element but array has 2
rust-analyzer E0527

Seems like a rust-analyzer bug. Please file an issue at Issues · rust-lang/rust-analyzer · GitHub

I replicated it with freshly installed Rust Analyzer 0.3.2971 (last update 2 days ago) on VS Code and can confirm this.

You can trust

cargo clippy/run

If compiler don't complain, you're practically good.
I have a few suspicion about it:

  • It's a Rust Analyzer bug,
  • Maybe version mismatch of RA (less likely) since destructuring assigment was stabilized around 1.59-ish

If i were to give an advice, try to submit a bug as @theemathas said (someone already linked the existing issue), and try to rely more on the compiler messages instead for checking your code
.

Thank you for your reply. It is very helpful for a beginner like me.

Have you encountered this issue before as well? If so, then it is not a problem with my local setup.Thank you for your reply!

What does this mean? I don’t quite understand. Has this bug been fixed? But I believe I am already using the latest version.

The bug will get fixed when the pull request is merged. The pull request is now open, so it's not merged. It will take some time until someone merges the pull request.