Skip to content

unhelpful "expected item" error when putting let binding outside of function #61764

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
matthiaskrgr opened this issue Jun 12, 2019 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Jun 12, 2019

let s = String::new();

fn main()  {
    println!("Hello world");
}
   Compiling playground v0.0.1 (/playground)
error: expected item, found keyword `let`
 --> src/main.rs:1:1
  |
1 | let s = String::new();
  | ^^^ expected item

Could we have some suggestions what kind of item should be put there? :)
Also there seems to be no error code which is strange..

rustc 1.37.0-nightly (02564de47 2019-06-10)

@matthiaskrgr
Copy link
Member Author

@rustbot modify labels: A-diagnostics

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 12, 2019
@jonas-schievink jonas-schievink added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jun 12, 2019
@estebank estebank added the A-parser Area: The lexing & parsing of Rust source code to an AST label Jun 12, 2019
@estebank
Copy link
Contributor

Also there seems to be no error code which is strange

This is a parser error and those rarely have codes because the surface of things that could have gone wrong is much larger.

@matthiaskrgr
Copy link
Member Author

Since it works with const instead of let, maybe we could suggest this?

@estebank estebank added the D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. label Oct 7, 2019
@merriam
Copy link

merriam commented Feb 26, 2022

I am a newcommer to Rust and have a similar issue. In my case, the code is:

/ Why does uncommenting the below item give "error: expected item, found `[`?"
// [#should_panic]
fn test_bad_card() {
    let a = to_card(&"Ten of Spades");
}

The problem with the error message is probably the word 'item'. Is this expected 'function, struct, or enum item' or 'top level item' or is there some other term?

Also, if you spot my error, let me know ;)

@dawnofmidnight
Copy link
Contributor

dawnofmidnight commented Jun 27, 2023

Since it works with const instead of let, maybe we could suggest this?

The compiler now suggests this (although only for immutable bindings):

error: expected item, found keyword `let`
 --> src/lib.rs:1:1
  |
1 | let s = String::new();
  | ^^^ consider using `const` or `static` instead of `let` for global variables

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-parser Area: The lexing & parsing of Rust source code to an AST C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

6 participants