小编eld*_*orz的帖子

为什么使用匹配时参数的顺序很重要!宏?

我试图将存储在结构向量中的枚举类型变量与作为参数传递到我的函数中的相同类型的变量进行比较。因此,两个枚举都存储在变量中。然而,我得到了意想不到的结果。我使用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)

comparison enums rust

1
推荐指数
1
解决办法
301
查看次数

标签 统计

comparison ×1

enums ×1

rust ×1