Skip to content

Recover from accidental usage of let in struct definition #101683

New issue

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

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

Already on GitHub? # to your account

Closed
Rageking8 opened this issue Sep 11, 2022 · 2 comments · Fixed by #101789
Closed

Recover from accidental usage of let in struct definition #101683

Rageking8 opened this issue Sep 11, 2022 · 2 comments · Fixed by #101789
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Rageking8
Copy link
Contributor

Rageking8 commented Sep 11, 2022

Given the following code: link

struct Struct {
    let x: i32,
}

fn main() {

}

The current output is:

   Compiling playground v0.0.1 (/playground)
error: expected identifier, found keyword `let`
 --> src/main.rs:2:5
  |
2 |     let x: i32,
  |     ^^^ expected identifier, found keyword

error: could not compile `playground` due to previous error

Since normal variable declaration are done using a let or other keywords in front and I accidentally made that typo due to muscle memory from creating regular variables. I think the ideal output for such a typo will be to suggest removing the let and having a small note stating that let is not required for struct variable declarations. (Note: the small note can be omitted if not necessary). This may be useful for beginners first getting used to the language's syntax.

@Rageking8 Rageking8 added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 11, 2022
@ghost
Copy link

ghost commented Sep 11, 2022

@rustbot claim

@rustbot rustbot assigned ghost Sep 11, 2022
@ghost
Copy link

ghost commented Sep 14, 2022

I feel like adding Kotlin into the diagnostic message also might help the developer.

Kotlin "structs" are defined like

data class Cookies(val recipe: SecretRecipe, val cook: GrandmaInstance)

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 10, 2022
`let`'s not needed in struct field definitions

Fixes rust-lang#101683
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Oct 10, 2022
`let`'s not needed in struct field definitions

Fixes rust-lang#101683
@bors bors closed this as completed in 01a2246 Oct 11, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant