diff options
| author | dianne <diannes.gm@gmail.com> | 2025-04-18 15:06:26 -0700 |
|---|---|---|
| committer | dianne <diannes.gm@gmail.com> | 2025-04-24 15:02:44 -0700 |
| commit | 43133184432fa3799ce504bbd34e17b120d7cd21 (patch) | |
| tree | 3dc83039f368eb99f65192056022503c1942e72f | |
| parent | 0eb3b110f0b14b21de2e7c283ea21bc86e0d311d (diff) | |
| download | rust-43133184432fa3799ce504bbd34e17b120d7cd21.tar.gz rust-43133184432fa3799ce504bbd34e17b120d7cd21.zip | |
update unstable book to mention moving out of boxes
| -rw-r--r-- | src/doc/unstable-book/src/language-features/deref-patterns.md | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/src/doc/unstable-book/src/language-features/deref-patterns.md b/src/doc/unstable-book/src/language-features/deref-patterns.md index d0a64538e8c..0cc7106da48 100644 --- a/src/doc/unstable-book/src/language-features/deref-patterns.md +++ b/src/doc/unstable-book/src/language-features/deref-patterns.md @@ -7,7 +7,7 @@ The tracking issue for this feature is: [#87121] ------------------------ > **Note**: This feature is incomplete. In the future, it is meant to supersede -> [`box_patterns`](./box-patterns.md) and [`string_deref_patterns`](./string-deref-patterns.md). +> [`box_patterns`] and [`string_deref_patterns`]. This feature permits pattern matching on [smart pointers in the standard library] through their `Deref` target types, either implicitly or with explicit `deref!(_)` patterns (the syntax of which @@ -54,6 +54,17 @@ if let [b] = &mut *v { assert_eq!(v, [Box::new(Some(2))]); ``` +Like [`box_patterns`], deref patterns may move out of boxes: + +```rust +# #![feature(deref_patterns)] +# #![allow(incomplete_features)] +struct NoCopy; +// Match exhaustiveness analysis is not yet implemented. +let deref!(x) = Box::new(NoCopy) else { unreachable!() }; +drop::<NoCopy>(x); +``` + Additionally, when `deref_patterns` is enabled, string literal patterns may be written where `str` is expected. Likewise, byte string literal patterns may be written where `[u8]` or `[u8; _]` is expected. This lets them be used in `deref!(_)` patterns: @@ -75,4 +86,6 @@ match *"test" { Implicit deref pattern syntax is not yet supported for string or byte string literals. +[`box_patterns`]: ./box-patterns.md +[`string_deref_patterns`]: ./string-deref-patterns.md [smart pointers in the standard library]: https://doc.rust-lang.org/std/ops/trait.DerefPure.html#implementors |
