Skip to content

Commit c4b85e3

Browse files
cuviperkpreid
andauthored
Update the min_exhaustive_patterns section
Co-authored-by: Kevin Reid <[email protected]>
1 parent 9d8b876 commit c4b85e3

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

posts/2024-10-17-Rust-1.82.0.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,33 +57,33 @@ The Rust target `aarch64-apple-darwin` for macOS on ARM64 (M1-family or later Ap
5757

5858
[The targets](https://doc.rust-lang.org/nightly/rustc/platform-support/apple-ios-macabi.html) are now tier 2, and can be downloaded with `rustup target add aarch64-apple-ios-macabi x86_64-apple-ios-macabi`, so now is an excellent time to update your CI pipeline to test that your code also runs in iOS-like environments.
5959

60-
### Minimal exhaustive patterns
60+
### Omitting empty types in pattern matching
6161

62-
Empty patterns can now be omitted in common cases:
62+
Patterns which match empty (a.k.a. uninhabited) types by value can now be omitted:
6363

6464
```rust
6565
use std::convert::Infallible;
66-
pub fn safe_unwrap<T>(x: Result<T, Infallible>) -> T {
66+
pub fn unwrap_without_panic<T>(x: Result<T, Infallible>) -> T {
6767
let Ok(x) = x; // the `Err` case does not need to appear
6868
x
6969
}
7070
```
7171

7272
This works with empty types such as a variant-less `enum Void {}`, or structs and enums with a visible empty field and no `#[non_exhaustive]` attribute. It will also be particularly useful in combination with the never type `!`, although that type is still unstable at this time.
7373

74-
This feature is "minimal" because there are still some exclusions at this time. For reasons related to uninitialized values and unsafe code, omitting patterns is not allowed if the empty type is accessed through a reference, pointer, or union field:
74+
There are still some cases where empty patterns must still be written. For reasons related to uninitialized values and unsafe code, omitting patterns is not allowed if the empty type is accessed through a reference, pointer, or union field:
7575

7676
```rust
77-
pub fn safe_unwrap_ref<T>(x: &Result<T, Infallible>) -> &T {
77+
pub fn unwrap_ref_without_panic<T>(x: &Result<T, Infallible>) -> &T {
7878
match x {
7979
Ok(x) => x,
80-
// this branch cannot be omitted because of the reference
80+
// this arm cannot be omitted because of the reference
8181
Err(infallible) => match *infallible {},
8282
}
8383
}
8484
```
8585

86-
To avoid interfering with crates that wish to support several rust versions, branches with empty patterns are not yet warned as "unreachable", despite the fact that they can be removed.
86+
To avoid interfering with crates that wish to support several Rust versions, `match` arms with empty patterns are not yet reported as unreachable code” warnings, despite the fact that they can be removed.
8787

8888
### Floating-point NaN semantics and `const`
8989

0 commit comments

Comments
 (0)