Skip to content

feature(destructuring_assignment) suggested errorneously #90095

Closed

Description

@moxian

(playground)

fn main(){
  let foo: [u8; _] = [1,2,3];
}
error[E0658]: destructuring assignments are unstable
 --> src/main.rs:2:17
  |
2 |   let foo: [u8; _] = [1,2,3];
  |                 ^
  |
  = note: see issue #71126 <https://github.com/rust-lang/rust/issues/71126> for more information
  = help: add `#![feature(destructuring_assignment)]` to the crate attributes to enable

error: in expressions, `_` can only be used on the left-hand side of an assignment
 --> src/main.rs:2:17
  |
2 |   let foo: [u8; _] = [1,2,3];
  |                 ^ `_` not allowed here

For more information about this error, try `rustc --explain E0658`.

However the #![feature(destructuring_assignment)] seems to be completely irrelevant here, and indeed adding that (playground)

#![feature(destructuring_assignment)]
fn main(){
  let foo: [u8; _] = [1,2,3];
}

still gives error of

error: in expressions, `_` can only be used on the left-hand side of an assignment
 --> src/main.rs:3:17
  |
3 |   let foo: [u8; _] = [1,2,3];
  |                 ^ `_` not allowed here

(Tangentially: technically the error message is wrong since _ is used in the left hand side of an assignment. Just not as the variable name as rustc expected... )

cc #71126

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 20, 2021
  2. moxian commented on Oct 20, 2021

    @moxian
    ContributorAuthor

    ah, wait, reading closer , #71126 (comment) implies that this behavior is expected, even if maybe unfortunate. I'll leave this bug report open for now, but feel free to close as WAI if that is the correct reading.

  3. rami3l commented on Oct 21, 2021

    @rami3l
    Member

    Encountered a similar case today, and this seems related to rust-lang/rfcs#2545.

  4. varkor commented on Oct 29, 2021

    @varkor
    Contributor

    The behaviour is correct, but a little confusingly-worded. By "left-hand side of an assignment", it means the variable being assigned to, rather than any syntax to the left of =. If you can think of better wording that makes this clearer, it would be good to clear this up!

  5. rami3l commented on Oct 29, 2021

    @rami3l
    Member

    @varkor According to the Reference:

    The assigned place operand must be a place expression...

    ... maybe here it's better to say:

    error: in expressions, _ can only be used in the place expression being assigned to

  6. varkor commented on Oct 29, 2021

    @varkor
    Contributor

    We have to be careful to use terminology most users will understand, and I think "place expression" is jargon that not a lot of users will be familiar with.

  7. rami3l commented on Oct 30, 2021

    @rami3l
    Member

    @varkor I understand this but it's hard to be both precise and straightforward... Let me try again:

    error: in expressions, _ can only be used in the left-hand side expression being assigned to

    Another possible version, but I'm not sure if this breaks the original meaning or not:

    error: _ can be a part of an expression only when the expression is being assigned to

    As a user, I think the predicate "in expressions" confuses me, since I'm using _ in a type signature, but rustc is telling me something about expressions.

    PS: I believe this problem is temporary. I guess once the array size can be omitted, this error will be always talking about the variable being assigned to (or does it?)

  8. araruna commented on Nov 7, 2021

    @araruna

    Sorry to be using this issue's comments section to ask this, but wasn't this the syntax used to ask the compiler to figure out the size of the array foo, being free from having to change that value in code whenever the assigned-from array literal changed?

    In other words, I thought this was legal code:

    fn main(){
      let foo: [u8; _] = [1,2,3];
    }

    I'm very confused... How am I supposed to write that, then?

  9. rami3l commented on Nov 7, 2021

    @rami3l
    Member

    @araruna This error message is hopefully a temporary state and will probably be fixed with rust-lang/rfcs#2545.
    However, we still have to do something about the error message to better the user experience in this period.

  10. cyrgani commented on Apr 16, 2026

    @cyrgani
    Contributor

    This code now compiles since #141610.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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