-
-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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 operatorDesired 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.2Anything 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.