docs

#E0103: 看起来像变体的绑定

模式中单独的名称是一个匹配所有值的新变量,除非它恰好是被匹配枚举的变体。这个名称是常量或静态变量,或者与某个变体的名称相近,因此它会绑定所有值。写出变体的路径 Color::Red,或者在守卫中比较:x if x == LIMIT。

nabi
const LIMIT: i32 = 10;

let n = 3;
match n {
    LIMIT => println("at the limit"),
    _ => println("below"),
}
error[E0103]: `LIMIT` here would be a new variable that matches every value, not the const `LIMIT`
  --> main.nabi:5:5
   |
 5 |     LIMIT => println("at the limit"),
   |     ^^^^^
   |
   = help: to compare with the const, write `x if x == LIMIT`

在守卫中与常量比较:

nabi
const LIMIT: i32 = 10;

let n = 3;
match n {
    m if m == LIMIT => println("at the limit"),
    _ => println("below"),
}
below

相关错误码:E0029 找不到枚举变体,E0099 不可达的 match 分支

发现错误? 在 GitHub 上编辑此页 最后更新: