【发布时间】:2021-10-28 11:00:12
【问题描述】:
我正在尝试将存储在结构向量中的枚举类型变量与作为参数传递给我的函数的相同类型的变量进行比较。因此,两个枚举都存储在变量中。但是,我得到了意想不到的结果。我正在使用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") }
}
输出:
expected
not expected 2
not expected 3
【问题讨论】:
标签: rust enums comparison