docs

#E0103: 看起來像變體的繫結

模式中單獨的名稱是一個比對所有值的新變數,除非它恰好是被比對列舉的某個變體。這個名稱是 const 或 static,或與某個變體的名稱相近,所以它會繫結所有值。請寫出變體的路徑 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 上編輯此頁 最後更新: