Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 47f092b

Browse files
authoredMar 6, 2025
Rollup merge of rust-lang#137358 - dianne:new-match-ergonomics-examples, r=Nadrieril
Match Ergonomics 2024: add context and examples to the unstable book The examples here are pretty limited and don't illustrate the differences between the two feature gates, but my hope is that they get the general idea across. I can try and add some more nuance or more comprehensive examples too if that would help. Hopefully the doctest isn't too sneaky. I wanted to make the bindings' types explicit, and the most readable way I could think of was to use a helper. ~~Unfortunately it looks like the "run this code" button doesn't work yet, but I made sure the examples are cross-edition, so that should resolve on its own once playground's nightly updates (or if playground's default becomes edition 2024, or if the edition in the markdown gets forwarded to playground).~~ It looks like the default edition on playground is now 2024, so the run button works! There's no output, but having a button to show that it compiles is nice, I think. Relevant tracking issue: rust-lang#123076 r? ````@Nadrieril````
2 parents 7ac42be + 812b1de commit 47f092b

File tree

2 files changed

+47
-6
lines changed

2 files changed

+47
-6
lines changed
 

‎src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024-structural.md

+23-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,32 @@ The tracking issue for this feature is: [#123076]
99
This feature is incomplete and not yet intended for general use.
1010

1111
This implements experimental, Edition-dependent match ergonomics under consideration for inclusion
12-
in Rust.
13-
For more information, see the corresponding typing rules for [Editions 2021 and earlier] and for
14-
[Editions 2024 and later].
12+
in Rust, allowing `&` patterns in more places. For example:
13+
```rust,edition2024
14+
#![feature(ref_pat_eat_one_layer_2024_structural)]
15+
#![allow(incomplete_features)]
16+
#
17+
# // Tests type equality in a way that avoids coercing `&&T` or `&mut T` to `&T`.
18+
# trait Eq<T> {}
19+
# impl<T> Eq<T> for T {}
20+
# fn has_type<T>(_: impl Eq<T>) {}
21+
22+
// `&` can match against a `ref` binding mode instead of a reference type:
23+
let (x, &y) = &(0, 1);
24+
has_type::<&u8>(x);
25+
has_type::<u8>(y);
26+
27+
// `&` can match against `&mut` references:
28+
let &z = &mut 2;
29+
has_type::<u8>(z);
30+
```
31+
32+
For specifics, see the corresponding typing rules for [Editions 2021 and earlier] and for
33+
[Editions 2024 and later]. For more information on binding modes, see [The Rust Reference].
1534

1635
For alternative experimental match ergonomics, see the feature
1736
[`ref_pat_eat_one_layer_2024`](./ref-pat-eat-one-layer-2024.md).
1837

1938
[Editions 2021 and earlier]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAQIBAQEBAAAAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=false
2039
[Editions 2024 and later]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAgEBAQEBAgIAAAAAAAAAAAAAAAA%3D&mode=rules&do_cmp=false
40+
[The Rust Reference]: https://doc.rust-lang.org/reference/patterns.html#binding-modes

‎src/doc/unstable-book/src/language-features/ref-pat-eat-one-layer-2024.md

+24-3
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,33 @@ The tracking issue for this feature is: [#123076]
99
This feature is incomplete and not yet intended for general use.
1010

1111
This implements experimental, Edition-dependent match ergonomics under consideration for inclusion
12-
in Rust.
13-
For more information, see the corresponding typing rules for [Editions 2021 and earlier] and for
14-
[Editions 2024 and later].
12+
in Rust, allowing `&` patterns in more places. For example:
13+
14+
```rust,edition2024
15+
#![feature(ref_pat_eat_one_layer_2024)]
16+
#![allow(incomplete_features)]
17+
#
18+
# // Tests type equality in a way that avoids coercing `&&T` or `&mut T` to `&T`.
19+
# trait Eq<T> {}
20+
# impl<T> Eq<T> for T {}
21+
# fn has_type<T>(_: impl Eq<T>) {}
22+
23+
// `&` can match against a `ref` binding mode instead of a reference type:
24+
let (x, &y) = &(0, 1);
25+
has_type::<&u8>(x);
26+
has_type::<u8>(y);
27+
28+
// `&` can match against `&mut` references:
29+
let &z = &mut 2;
30+
has_type::<u8>(z);
31+
```
32+
33+
For specifics, see the corresponding typing rules for [Editions 2021 and earlier] and for
34+
[Editions 2024 and later]. For more information on binding modes, see [The Rust Reference].
1535

1636
For alternative experimental match ergonomics, see the feature
1737
[`ref_pat_eat_one_layer_2024_structural`](./ref-pat-eat-one-layer-2024-structural.md).
1838

1939
[Editions 2021 and earlier]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAQIBAQABAAAAAQEBAAEBAAABAAA%3D&mode=rules&do_cmp=false
2040
[Editions 2024 and later]: https://nadrieril.github.io/typing-rust-patterns/?compare=false&opts1=AQEBAAABAQABAgIAAQEBAAEBAAABAAA%3D&mode=rules&do_cmp=false
41+
[The Rust Reference]: https://doc.rust-lang.org/reference/patterns.html#binding-modes

0 commit comments

Comments
 (0)
Failed to load comments.