0

Why does this code give me strange results?

const VAR1: u16 = 1;
const VAR2: u16 = 2;
const VAR3: u16 = 3;
const VAR4: u16 = 4;

#[cfg(test)]
mod tests {
    // use {VAR1, VAR2, VAR3, VAR4};
    #[test]
    fn test_match() {
        let a = 4;
        match a {
            VAR1 => {
                println!("matched: 1");
            }
            VAR2 => {
                println!("matched: 2");
            }
            VAR3 => {
                println!("matched: 3");
            }
            VAR4 => {
                println!("matched: 4");
            }
            _ => {
                println!("not matched");
            }
        }
    }
}

When the line // use {VAR1, VAR2, VAR3, VAR4}; is commented, then the result of test execution is matched: 1. When use {VAR1, VAR2, VAR3, VAR4}; is not commented, then the result is correct and is matched: 4.

Why the compiler doesn't fail in the first case ?

cargo test -- --nocapture

rustc --version
rustc 1.16.0-nightly (7e38a89a7 2017-01-06)

You can find the test project code here https://github.com/asmsoft/rust-match-test

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
  • 2
    tl;dr: if there is no constant with the given name in scope, rust *binds* the value you pass into the `match` (the `match` discriminant) to the new name. Just like a `let name = value;` statement! That's that confused OP in the linked question as well. But now I'm curious why the Rust compiler doesn't complain about "unreachable pattern" in your case... – Lukas Kalbertodt May 11 '17 at 20:23

1 Answers1

5

If you don't use VAR1, VAR2, VAR3, VAR4, they are not in the scope of the match, and the pattern becomes a binding. The remaining patterns become unreachable and you have the following warning:

  --> src/main.rs:28:13
   |
28 |             VAR4 => {
   |             ^^^^
   |
   = note: #[warn(unused_variables)] on by default

warning: unreachable pattern

If you replaced your print with:

match a {
    VAR1 => {
        println!("matched: 1 ({:?})", VAR1);
    }
}

as VAR1 now have the value it matched against, it will print

matched: 1 (4)

With the use, the four constants are in scope and you can match against them.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
Francois Mockers
  • 713
  • 4
  • 14