## Refutability（可反驳性）: 模式是否会匹配失效

ch18-02-refutability.md
commit 0c2d41e2afce734825c3a12087d423e8c2f0ae53

``````fn main() {
let some_option_value: Option<i32> = None;
let Some(x) = some_option_value;
}
``````

``````\$ 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: `Option<i32>` defined here
= 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 |     let x = if let Some(x) = some_option_value { x } else { todo!() };
|     ++++++++++                                 ++++++++++++++++++++++

error: could not compile `patterns` due to previous error
``````

``````fn main() {
let some_option_value: Option<i32> = None;
if let Some(x) = some_option_value {
println!("{}", x);
}
}
``````

``````fn main() {
if let x = 5 {
println!("{}", x);
};
}
``````

Rust 会抱怨将不可反驳模式用于 `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
``````
{"ft_lang_label":"__label__zh","ft_lang_prob":0.74166644,"math_prob":0.90726674,"size":2926,"snap":"2022-40-2023-06","text_gpt3_token_len":1593,"char_repetition_ratio":0.1386037,"word_repetition_ratio":0.066066064,"special_character_ratio":0.27580315,"punctuation_ratio":0.11910112,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9607904,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-01-31T13:15:04Z\",\"WARC-Record-ID\":\"<urn:uuid:41db815c-c831-4109-83e9-fdfccc543600>\",\"Content-Length\":\"33469\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:a4adad88-0dc1-4a3b-a310-33318c7cdd5f>\",\"WARC-Concurrent-To\":\"<urn:uuid:0a416949-9538-418a-8e4a-ce79ba47f939>\",\"WARC-IP-Address\":\"185.199.111.153\",\"WARC-Target-URI\":\"https://kaisery.github.io/trpl-zh-cn/ch18-02-refutability.html\",\"WARC-Payload-Digest\":\"sha1:4TXTUR7FRCDC5N7E5ZD5I2H42ZBXEO3P\",\"WARC-Block-Digest\":\"sha1:Y25RIBQ3CIJNYFHF5P2UV6WQMGS4MF25\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764499871.68_warc_CC-MAIN-20230131122916-20230131152916-00703.warc.gz\"}"}