1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-03-22 23:05:22 +02:00

Update if-let-expressions.md (#540)

* Update if-let-expressions.md

add a note about let-else expressions.  Closes #536

* Remove old mention of let-else

* Indent code block to match the bullet point

---------

Co-authored-by: Martin Geisler <martin@geisler.net>
This commit is contained in:
Yuri Astrakhan 2023-04-03 06:04:26 -04:00 committed by GitHub
parent 4527d5f3de
commit acf2990243
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -19,8 +19,21 @@ Rust.
<details>
* `if let` can be more concise than `match`, e.g., when only one case is interesting. In contrast, `match` requires all branches to be covered.
* For the similar use case consider demonstrating a newly stabilized [`let else`](https://github.com/rust-lang/rust/pull/93628) feature.
* A common usage is handling `Some` values when working with `Option`.
* Unlike `match`, `if let` does not support guard clauses for pattern matching.
* Since 1.65, a similar [let-else](https://doc.rust-lang.org/rust-by-example/flow_control/let_else.html) construct allows to do a destructuring assignment, or if it fails, have a non-returning block branch (panic/return/break/continue):
```rust,editable
fn main() {
println!("{:?}", second_word_to_upper("foo bar"));
}
fn second_word_to_upper(s: &str) -> Option<String> {
let mut it = s.split(' ');
let (Some(_), Some(item)) = (it.next(), it.next()) else {
return None;
};
Some(item.to_uppercase())
}
</details>