You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-18 23:27:34 +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:
@ -19,8 +19,21 @@ Rust.
|
|||||||
<details>
|
<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.
|
* `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`.
|
* A common usage is handling `Some` values when working with `Option`.
|
||||||
* Unlike `match`, `if let` does not support guard clauses for pattern matching.
|
* 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>
|
</details>
|
||||||
|
Reference in New Issue
Block a user