Skip to content

Commit d412694

Browse files
committed
apply unstable book suggestions
- Clarifies the uses of implicit and explicit deref patterns - Adapts the example to provide a use for explicit `deref!(_)` - Replaces binding mode annotations with refs on the scrutinee - Cross-links with `string_deref_patterns` and `box_patterns` The example is still contrived, but hopefully the intent comes across.
1 parent 64c7917 commit d412694

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

Diff for: src/doc/unstable-book/src/language-features/box-patterns.md

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The tracking issue for this feature is: [#29641]
66

77
------------------------
88

9+
> **Note**: This feature will be superseded by [`deref_patterns`] in the future.
10+
911
Box patterns let you match on `Box<T>`s:
1012

1113

@@ -28,3 +30,5 @@ fn main() {
2830
}
2931
}
3032
```
33+
34+
[`deref_patterns`]: ./deref-patterns.md

Diff for: src/doc/unstable-book/src/language-features/deref-patterns.md

+24-11
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,34 @@ The tracking issue for this feature is: [#87121]
66

77
------------------------
88

9-
This feature permits pattern matching on [library-defined smart pointers] through their `Deref`
10-
target types, either implicitly or with the placeholder `deref!(_)` syntax.
9+
> **Note**: This feature is incomplete. In the future, it is meant to supersede
10+
> [`box_patterns`](./box-patterns.md) and [`string_deref_patterns`](./string-deref-patterns.md).
11+
12+
This feature permits pattern matching on [smart pointers in the standard library] through their
13+
`Deref` target types, either implicitly or with explicit `deref!(_)` patterns (the syntax of which
14+
is currently a placeholder).
1115

1216
```rust
1317
#![feature(deref_patterns)]
1418
#![allow(incomplete_features)]
1519

1620
pub fn main() {
1721
let mut v = vec![Box::new(Some(0))];
18-
if let [Some(ref mut x)] = v {
22+
23+
// Implicit dereferences are inserted when a pattern can match against the
24+
// result of repeatedly dereferencing but can't match against a smart
25+
// pointer itself. This works alongside match ergonomics for references.
26+
if let [Some(x)] = &mut v {
1927
*x += 1;
2028
}
21-
if let deref!([deref!(Some(ref mut x))]) = v {
22-
*x += 1;
29+
30+
// Explicit `deref!(_)` patterns may instead be used when finer control is
31+
// needed, e.g. to dereference only a single smart pointer, or to bind the
32+
// the result of dereferencing to a variable.
33+
if let deref!([deref!(opt_x @ Some(1))]) = &mut v {
34+
opt_x.as_mut().map(|x| *x += 1);
2335
}
36+
2437
assert_eq!(v, [Box::new(Some(2))]);
2538
}
2639
```
@@ -31,18 +44,18 @@ when matching on nested structures:
3144
```rust
3245
pub fn main() {
3346
let mut v = vec![Box::new(Some(0))];
34-
if let [ref mut b] = *v {
35-
if let Some(ref mut x) = **b {
47+
if let [b] = &mut *v {
48+
if let Some(x) = &mut **b {
3649
*x += 1;
3750
}
3851
}
39-
if let [ref mut b] = *v {
40-
if let Some(ref mut x) = **b {
41-
*x += 1;
52+
if let [b] = &mut *v {
53+
if let opt_x @ Some(1) = &mut **b {
54+
opt_x.as_mut().map(|x| *x += 1);
4255
}
4356
}
4457
assert_eq!(v, [Box::new(Some(2))]);
4558
}
4659
```
4760

48-
[library-defined smart pointers]: https://doc.rust-lang.org/std/ops/trait.DerefPure.html#implementors
61+
[smart pointers in the standard library]: https://doc.rust-lang.org/std/ops/trait.DerefPure.html#implementors

Diff for: src/doc/unstable-book/src/language-features/string-deref-patterns.md

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ The tracking issue for this feature is: [#87121]
66

77
------------------------
88

9+
> **Note**: This feature will be superseded by [`deref_patterns`] in the future.
10+
911
This feature permits pattern matching `String` to `&str` through [its `Deref` implementation].
1012

1113
```rust
@@ -42,4 +44,5 @@ pub fn is_it_the_answer(value: Value) -> bool {
4244
}
4345
```
4446

47+
[`deref_patterns`]: ./deref-patterns.md
4548
[its `Deref` implementation]: https://doc.rust-lang.org/std/string/struct.String.html#impl-Deref-for-String

0 commit comments

Comments
 (0)