@@ -65,15 +65,26 @@ let deref!(x) = Box::new(NoCopy) else { unreachable!() };
65
65
drop :: <NoCopy >(x );
66
66
```
67
67
68
- Additionally, when ` deref_patterns ` is enabled, string literal patterns may be written where ` str `
69
- is expected. Likewise, byte string literal patterns may be written where ` [u8] ` or ` [u8; _] ` is
70
- expected. This lets them be used in ` deref!(_) ` patterns:
68
+ Additionally, ` deref_patterns ` implements changes to string and byte string literal patterns,
69
+ allowing then to be used in deref patterns:
71
70
72
71
``` rust
73
72
# #![feature(deref_patterns)]
74
73
# #![allow(incomplete_features)]
75
- match (" test" . to_string (), b " test" . to_vec ()) {
76
- (deref! (" test" ), deref! (b " test" )) => {}
74
+ match (" test" . to_string (), Box :: from (" test" ), b " test" . to_vec ()) {
75
+ (" test" , " test" , b " test" ) => {}
76
+ _ => panic! (),
77
+ }
78
+
79
+ // This works through multiple layers of reference and smart pointer:
80
+ match (& Box :: new (& " test" . to_string ()), && & " test" ) {
81
+ (" test" , " test" ) => {}
82
+ _ => panic! (),
83
+ }
84
+
85
+ // `deref!("...")` syntax may also be used:
86
+ match " test" . to_string () {
87
+ deref! (" test" ) => {}
77
88
_ => panic! (),
78
89
}
79
90
@@ -82,10 +93,16 @@ match *"test" {
82
93
" test" => {}
83
94
_ => panic! (),
84
95
}
96
+ match * b " test" {
97
+ b " test" => {}
98
+ _ => panic! (),
99
+ }
100
+ match * (b " test" as & [u8 ]) {
101
+ b " test" => {}
102
+ _ => panic! (),
103
+ }
85
104
```
86
105
87
- Implicit deref pattern syntax is not yet supported for string or byte string literals.
88
-
89
106
[ `box_patterns` ] : ./box-patterns.md
90
107
[ `string_deref_patterns` ] : ./string-deref-patterns.md
91
108
[ smart pointers in the standard library ] : https://doc.rust-lang.org/std/ops/trait.DerefPure.html#implementors
0 commit comments