Skip to content
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

In what situation can a variable shadow a constant? #1365

Closed
your-diary opened this issue May 29, 2023 · 2 comments
Closed

In what situation can a variable shadow a constant? #1365

your-diary opened this issue May 29, 2023 · 2 comments

Comments

@your-diary
Copy link

According to Identifier patterns,

It is an error if ref or ref mut is specified and the identifier shadows a constant.

However, the code below (Playground) doesn't compile though it doesn't contain ref or ref mut.

const a: usize = 3;
fn main() {
    let a = 0;
}

In what situation can a variable without ref or ref mut shadow a constant?

@bjorn3
Copy link
Member

bjorn3 commented May 29, 2023

According to the rule right before the rule you quoted:

Path patterns take precedence over identifier patterns.

Your example attempts to match the constant a rather than binding it as variable. If you did something like

const a: () = ();
fn main() {
    let a = ();
}

it would have worked with a warning as now the constant a infallibly matches against the value (). It still doesn't shadow the constant.

warning: constant in pattern `a` should have an upper case name
 --> src/main.rs:3:9
  |
3 |     let a = ();
  |         ^ help: convert the identifier to upper case: `A`

@your-diary
Copy link
Author

@bjorn3 I understand it now. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants