File tree Expand file tree Collapse file tree 1 file changed +52
-0
lines changed
Expand file tree Collapse file tree 1 file changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -165,6 +165,58 @@ let &ref mut foo = &mut 42;
165165let _ : & mut u8 = foo ;
166166```
167167
168+ However, if the type of the scrutinee is unknown, an ` & ` pattern will still
169+ constrain inference to force it to be a shared reference.
170+
171+ ``` rust
172+ // ! All editions
173+ fn generic <R : Ref >() -> (R , bool ) {
174+ R :: meow ()
175+ }
176+
177+ trait Ref : Sized {
178+ fn meow () -> (Self , bool );
179+ }
180+
181+ impl Ref for & 'static [(); 0 ] {
182+ fn meow () -> (Self , bool ) {
183+ (& [], false )
184+ }
185+ }
186+
187+ impl Ref for & 'static mut [(); 0 ] {
188+ fn meow () -> (Self , bool ) {
189+ (& mut [], true )
190+ }
191+ }
192+
193+ fn main () {
194+ let (& _ , b ) = generic ();
195+ assert! (! b );
196+ }
197+ ```
198+
199+ ``` rust
200+ // ! All editions
201+ fn generic <R : Ref >() -> R {
202+ R :: meow ()
203+ }
204+
205+ trait Ref : Sized {
206+ fn meow () -> Self ;
207+ }
208+
209+ impl Ref for & 'static mut [(); 0 ] {
210+ fn meow () -> Self {
211+ & mut []
212+ }
213+ }
214+
215+ fn main () {
216+ let & _ = generic (); // ~ERROR[E0277]: the trait bound `&_: Ref` is not satisfied
217+ }
218+ ```
219+
168220## Edition 2024: ` & ` and ` &mut ` can match against inherited references
169221
170222When the default binding mode is ` ref ` or ` ref mut ` , ` & ` and ` &mut ` patterns can
You can’t perform that action at this time.
0 commit comments