rust-lang / rust-lang/rust

Better message when trying to assign two variables at once / match on a tuple without using parentheses

Open
#140,615 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Code
fn main() {
    let (x, y) = 10 / 5, 10 % 5;
}

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=b8d43356c6ba14b240ab12eea1f883da

Current output
error: expected one of `.`, `;`, `?`, `else`, or an operator, found `,`
 --> src/main.rs:2:24
  |
2 |     let (x, y) = 10 / 5, 10 % 5;
  |                        ^ expected one of `.`, `;`, `?`, `else`, or an operator
Desired output

Something along the lines of:

If trying to match a tuple on the left side, you have to create a tuple on the right side by adding parentheses.
Or, if providing the error already in the case where the left side isn’t a match on a tuple yet (see below): It could recognise that's what the user is trying to do and suggest creating a tuple on the right and match on it on the left "to assign two values at the same time".

Rationale and extra context

No response

Other cases

For this:


fn main() {
    let x, y = 10 / 5, 10 % 5;
}

Playground: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2024&gist=2cb37a7ed277f032bcae34c793cdfcfb

The error is already helpful by telling us how to match a tuple:

error: unexpected `,` in pattern
 --> src/main.rs:2:10
  |
2 |     let x, y = 10 / 5, 10 % 5;
  |          ^
  |
help: try adding parentheses to match on a tuple
  |
2 |     let (x, y) = 10 / 5, 10 % 5;
  |         +    +

Ideally, the error output of this code would already include the hint of how to achieve the whole thing (adding parentheses on both sides).

Rust Version
rustc 1.88.0-nightly (4824c2bb7 2025-05-02)
binary: rustc
commit-hash: 4824c2bb7445cb2478aab0190c268c939d77a0f6
commit-date: 2025-05-02
host: x86_64-unknown-linux-gnu
release: 1.88.0-nightly
LLVM version: 20.1.2
Anything else?

Motivation is this use case: https://stackoverflow.com/questions/69051429/what-is-the-function-to-get-the-quotient-and-remainder-divmod-for-rust
Also, the concern that it isn’t optimised away into one divmod if spread out across different lines.

Not a huge issue in case it's hard to recognise this case in the compiler. It’d just be a nicer error message helping to save a bit of time or helping someone not familiar with tuples and tuple destructuring.

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

Reproduce both examples in the Rust Playground and compare their current rustc diagnostics, especially the existing tuple-pattern hint. Trace the compiler diagnostic path for the unexpected comma in the shown code; done means the diagnostic explains or suggests adding parentheses on both sides to assign tuple values.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.