Description
Right now, it can be somewhat confusing what an item is when one gets an error like the one below. Most people who see this error likely don't know enough about Rust to know what an item is, making it fairly unhelpful. The let
example does have a better error (although notably not for let mut
), but that's only one particular case.
Code
5
Current output
error: expected item, found `5`
--> src/lib.rs:1:1
|
1 | 5
| ^ expected item
Desired output
error: expected item, found `5`
--> src/main.rs:1:1
|
1 | 5
| ^ expected item
|
| help: items are things that can appear in a module. for example:
| - global variables: `const`, `static`
| - types and traits: `struct`, `enum`, `trait`,
| - functions: `fn`, `async fn`
| - implementations: `impl`
| - module definitions and uses: `mod`, `use`
|
| for a full list of possible items see https://doc.rust-lang.org/reference/items.html.
The above "desired output" is not an exhaustive list. In particular, it doesn't include macros which expand to items, attributes, doc comments, unsafe
, extern
, and a number of other potentially relevant things. I'm not sure just how exhaustive this list should be, as it's a long one and preferably shouldn't overwhelm the reader.
Another consideration is whether a list is useful at all. There are a lot of things, and especially if one is making a mistake like this, it is entirely possible that they would not know what many of the things on this list are. A link to the reference is included here, but again, the reference isn't exactly an introductory resource that one reads to learn the language.
Other cases
error: expected item, found keyword `let`
--> src/lib.rs:1:1
|
1 | let x = 5;
| ^^^ consider using `const` or `static` instead of `let` for global variables
Anything else?
Other similar issues:
- unhelpful "expected item" error when putting let binding outside of function #61764: This issue is relevant, but particularly for let bindings.
- Provide a better error when code is put outside of a function #92615: This is also relevant, and the title is somewhat more general, but the description focuses on let bindings again, hence why a new issue was created.