我试图将存储在结构向量中的枚举类型变量与作为参数传递到我的函数中的相同类型的变量进行比较。因此,两个枚举都存储在变量中。然而,我得到了意想不到的结果。我使用matches!()
宏来进行比较。谁能解释这种行为?
enum Foo {
A,
B,
}
fn main() {
let a = Foo::A;
if matches!(a, Foo::A) { println!("expected") }
if matches!(a, Foo::B) { println!("not expected 1") }
if matches!(Foo::B, a) { println!("not expected 2") }
let b = Foo::B;
if matches!(a, b) { println!("not expected 3") }
}
Run Code Online (Sandbox Code Playgroud)
输出:
enum Foo {
A,
B,
}
fn main() {
let a = Foo::A;
if matches!(a, Foo::A) { println!("expected") }
if matches!(a, Foo::B) { println!("not expected 1") }
if …
Run Code Online (Sandbox Code Playgroud)