模式有兩種形式:refutable(可反駁的)和 irrefutable(不可反駁的)。能匹配任何傳遞的可能值的模式被稱為是 不可反駁的(irrefutable)。一個(gè)例子就是 let x = 5; 語句中的 x,因?yàn)?nbsp;x 可以匹配任何值所以不可能會(huì)失敗。對(duì)某些可能的值進(jìn)行匹配會(huì)失敗的模式被稱為是 可反駁的(refutable)。一個(gè)這樣的例子便是 if let Some(x) = a_value 表達(dá)式中的 Some(x);如果變量 a_value 中的值是 None 而不是 Some,那么 Some(x) 模式不能匹配。
函數(shù)參數(shù)、 let 語句和 for 循環(huán)只能接受不可反駁的模式,因?yàn)橥ㄟ^不匹配的值程序無法進(jìn)行有意義的工作。if let 和 while let 表達(dá)式被限制為只能接受可反駁的模式,因?yàn)楦鶕?jù)定義他們意在處理可能的失?。簵l件表達(dá)式的功能就是根據(jù)成功或失敗執(zhí)行不同的操作。
$ cargo run
Compiling patterns v0.1.0 (file:///projects/patterns)
error[E0005]: refutable pattern in local binding: `None` not covered
--> src/main.rs:3:9
|
3 | let Some(x) = some_option_value;
| ^^^^^^^ pattern `None` not covered
|
= note: `let` bindings require an "irrefutable pattern", like a `struct` or an `enum` with only one variant
= note: for more information, visit https://doc.rust-lang.org/book/ch18-02-refutability.html
= note: the matched value is of type `Option<i32>`
help: you might want to use `if let` to ignore the variant that isn't matched
|
3 | if let Some(x) = some_option_value { /* */ }
|
For more information about this error, try `rustc --explain E0005`.
error: could not compile `patterns` due to previous error
為了修復(fù)在需要不可反駁模式的地方使用可反駁模式的情況,可以修改使用模式的代碼:不同于使用 let,可以使用 if let。如此,如果模式不匹配,大括號(hào)中的代碼將被忽略,其余代碼保持有效。示例 18-9 展示了如何修復(fù)示例 18-8 中的代碼。
if let Some(x) = some_option_value {
println!("{}", x);
}
示例 18-9: 使用 if let 和一個(gè)帶有可反駁模式的代碼塊來代替 let
我們給了代碼一個(gè)得以繼續(xù)的出路!雖然我們沒辦法在避免產(chǎn)生錯(cuò)誤的情況下使用不可反駁模式,但這段使用可反駁模式的代碼是完全有效的。如果為 if let 提供了一個(gè)總是會(huì)匹配的模式,比如示例 18-10 中的 x,編譯器會(huì)給出一個(gè)警告:
if let x = 5 {
println!("{}", x);
};
示例 18-10: 嘗試把不可反駁模式用到 if let 上
Rust 會(huì)抱怨將不可反駁模式用于 if let 是沒有意義的:
$ cargo run
Compiling patterns v0.1.0 (file:///projects/patterns)
warning: irrefutable `if let` pattern
--> src/main.rs:2:8
|
2 | if let x = 5 {
| ^^^^^^^^^
|
= note: `#[warn(irrefutable_let_patterns)]` on by default
= note: this pattern will always match, so the `if let` is useless
= help: consider replacing the `if let` with a `let`
warning: `patterns` (bin "patterns") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.39s
Running `target/debug/patterns`
5
基于此,match匹配分支必須使用可反駁模式,除了最后一個(gè)分支需要使用能匹配任何剩余值的不可反駁模式。Rust允許我們?cè)谥挥幸粋€(gè)匹配分支的match中使用不可反駁模式,不過這么做不是特別有用,并可以被更簡(jiǎn)單的 let 語句替代。
更多建議: