rust-lang / rust-lang/rust

mut in slice pattern: `[_, mut @ ..] = &[...]` is not needed?

Open
#126,676 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-patterns A-slice-patterns T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Might be related to https://github.com/rust-lang/rust/issues/123076

Code
fn foo(s: &[usize]) {
    let [a, b @ ..] = s else {panic!()};
    let [a, mut b @ ..] = s else {panic!()};
}
Current output
error[E0277]: the size for values of type `[usize]` cannot be known at compilation time
 --> src/lib.rs:3:13
  |
3 |     let [a, mut b @ ..] = s else {panic!()};
  |             ^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `Sized` is not implemented for `[usize]`
  = note: all local variables must have a statically known size
  = help: unsized locals are gated as an unstable feature

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground` (lib) due to 1 previous error
Desired output
3 |     let [a, mut b @ ..] = s else {panic!()};
  |             ^^^^^^^^^^ doesn't have a size known at compile-time
                ^^^ help: remove this `mut` because ...
Rationale and extra context

I use mut b @ .. because I would like to change b later.
But removing mut makes it compile. So my thoughts are misleading.

Other cases

No response

Rust Version
Nightly version: 1.81.0-nightly

(2024-06-18 8fcd4dd08e2ba3e922d9)
Anything else?

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided slice-pattern snippet on the stated nightly compiler and compare the diagnostics for [_, b @ ..] and [_, mut b @ ..]. Trace the compiler diagnostic path for E0277 and determine where the requested help suggestion would be tested. Done means the invalid mut case reports a targeted suggestion while the valid case continues to compile.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.