Skip to content

Rules for matching against variable unclear. #28687

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

Closed
XAMPPRocky opened this issue Sep 27, 2015 · 3 comments
Closed

Rules for matching against variable unclear. #28687

XAMPPRocky opened this issue Sep 27, 2015 · 3 comments

Comments

@XAMPPRocky
Copy link
Member

When you have a match statement against a variable the behaviour is unexpected.

I tried this code:

fn main() {
    let x = 'x';
    let c = 'c';
    match c {
        x => println!("Bug? {} {}", x, c),
    }

    println!("x: {}", x)
}

I expected to to see this happen:

Either get an error telling me that placing a variable won't do what I think it will do. Or allow this to happen, and allow for specific cases, as currently adding any extra arms returns a unreachable pattern error.

Instead this happened:

This compiles with no errors, or warning, and prints out:

Bug? c c
x: x

Meta

rustc --version --verbose

rustc 1.3.0 (9a92aaf19 2015-09-15)
binary: rustc
commit-hash: 9a92aaf19a64603b02b4130fe52958cc12488900
commit-date: 2015-09-15
host: x86_64-apple-darwin
release: 1.3.0
@XAMPPRocky XAMPPRocky changed the title rules for matching against variable unclear. Rules for matching against variable unclear. Sep 27, 2015
@bluss
Copy link
Member

bluss commented Sep 27, 2015

You can match against patterns that are composed of: Literals, ranges, Struct constructors, Enum variant constructors and const constants.

If you put in an identifier like this it's just a new binding (a catch-all). Rust generally allows variable shadowing without warning (let x = 1; let x = x + 1 is ok), so this is consistent with that at least.

@bluss
Copy link
Member

bluss commented Sep 27, 2015

Relevant doc section is Patterns. I think it could be clarified around this issue.

@steveklabnik
Copy link
Member

tagging with docs to see if i can make this somehow more prominent, but I'm not sure.

steveklabnik added a commit to steveklabnik/rust that referenced this issue Oct 7, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Oct 8, 2015
steveklabnik added a commit to steveklabnik/rust that referenced this issue Oct 8, 2015
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

3 participants