<分区>
为什么这段代码会给我奇怪的结果?
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");
}
}
}
}
当 //use {VAR1, VAR2, VAR3, VAR4};
行被注释时,测试执行的结果是matched: 1
。当use {VAR1, VAR2, VAR3, VAR4};
没有注释时,结果是正确的,并且是匹配:4
。
为什么编译器在第一种情况下不会失败?
cargo 测试 -- --nocapture
rustc --version
rustc 1.16.0-nightly (7e38a89a7 2017-01-06)
您可以在这里找到测试项目代码https://github.com/asmsoft/rust-match-test