You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Matching a slice against a [slice pattern][patterns.slice] other than one with only a single [rest pattern][patterns.rest] (i.e. `[..]`) is treated as a read of the length from the slice and captures the slice by `ImmBorrow`.
380
380
381
-
```rust,compile_fail,E0502
382
-
let x: &mut [u8] = &mut [];
383
-
let c = || match x { // Captures `*x` by `ImmBorrow`.
384
-
&mut [] => (),
385
-
// ^^
386
-
// This matches a slice of exactly zero elements. To know whether the
387
-
// scrutinee matches, the length must be read, causing the slice to
388
-
// be captured.
389
-
_ => (),
390
-
};
391
-
let _ = &mut *x; // ERROR: Cannot borrow `*x` as mutable.
392
-
c();
393
-
```
394
-
395
-
```rust,no_run
396
-
let x: &mut [u8] = &mut [];
397
-
let c = || match x { // Does not capture `*x`.
398
-
[..] => (),
399
-
// ^^ Rest pattern.
400
-
};
401
-
let _ = &mut *x; // OK: `*x` can be borrow here.
402
-
c();
403
-
```
381
+
> [!NOTE]
382
+
> Perhaps surprisingly, even though the length is contained in the (wide) *pointer* to the slice, it is the place of the *pointee* (the slice) that is treated as read and is captured.
0 commit comments