Skip to content

Diagnosis of incorrect operator syntax could be more helpful #149805

Description

@TomNicholas

Code

fn main() {
    let numbers = vec![1, 2, 3, 4, 5];
    let threshold = 3;

    // This should use >= but mistakenly uses =>
    let position = numbers.iter().position(|x| x => &threshold);

    println!("{:?}", position);
}

Current output

Error: Exit code 1
error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `=>`
 --> minimal_example.rs:9:50
  |
9 |     let position = numbers.iter().position(|x| x => &threshold);
  |                                                  ^^ expected one of 8 possible tokens

error: aborting due to 1 previous error

Desired output

error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `{`, or an operator, found `=>`
 --> minimal_example.rs:9:50
  |
9 |     let position = numbers.iter().position(|x| x => &threshold);
  |                                                  ^^ 
  |                                                  |
  |                                                  unexpected token
  |                                                  help: did you mean `>=`?

Rationale and extra context

This kind of simple syntax mistake should be straightforward to identify, and the suggestion is simple. But it's still useful for those who are new to the language (or for LLM assistants).

This type of "did you mean" suggestion seems in line with other similar suggestions the compiler makes. The only possible difference I can see is that this occurs during tokenisation rather than at some later stage.

Other cases

Rust Version

$ rustc --version --verbose
rustc 1.91.0 (f8297e351 2025-10-28)
binary: rustc
commit-hash: f8297e351a40c1439a467bbbb6879088047f50b3
commit-date: 2025-10-28
host: aarch64-apple-darwin
release: 1.91.0
LLVM version: 21.1.2

Anything else?

No response

Metadata

Metadata

Assignees

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
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions