Skip to content

Better error messages: Tell programmer about forgotten let #101880

Closed
@Kaligule

Description

@Kaligule

As a python programmer, forgetting a let in an assignment to a variable that didn't exist before is an easy mistake to make.

example code

fn main() {
    // forgot the "let" here
    horses = ["Secretariat", "Seabiscuit", "Binky"];
    println!("I have {} horses.", horses.len());
}

(link to playground)

Rust sees two uses of an undefined variable here (E0425 twice):

error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `horses` in this scope
 --> src/main.rs:3:5
  |
3 |     horses = ["Secretariat", "Seabiscuit", "Binky"];
  |     ^^^^^^ not found in this scope
error[[E0425]](https://doc.rust-lang.org/stable/error-index.html#E0425): cannot find value `horses` in this scope
 --> src/main.rs:4:35
  |
4 |     println!("I have {} horses.", horses.len());
  |                                   ^^^^^^ not found in this scope

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

What I would hope for

While the second one is fine, in the first case I would have really appreciated a sentence like this:

Did you mean to use `let` to introduce a new variable? Try `let horses = ...`

This would helped me as a newcomer to the language, because it does an educated guess what the problem might be and tells me how to fix it.

I think the hint should be added if the value is assigned to (first error), but not if it is used as an argument (second error).

I hope this is the right place and format to file this suggestion, I would appreciate hints if this is not the case.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-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