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
Copy file name to clipboardExpand all lines: src/types/closure.md
+8-13Lines changed: 8 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -98,8 +98,7 @@ Async closures always capture all input arguments, regardless of whether or not
98
98
## Capture Precision
99
99
100
100
r[type.closure.capture.precision.capture-path]
101
-
A *capture path* is a sequence starting with a variable from the environment followed by zero or more place projections that were applied to that variable, as well as
102
-
any [further projections performed by matching against patterns][pattern-wildcards].
101
+
A *capture path* is a sequence starting with a variable from the environment followed by zero or more place projections that were applied to that variable, as well as any [further projections performed by matching against patterns][pattern-wildcards].
If [the `#[non_exhaustive]` attribute][non_exhaustive] is applied to an enum
309
-
defined in an external crate, it is considered to have multiple variants,
310
-
even if only one variant is actually present.
307
+
If [the `#[non_exhaustive]` attribute][non_exhaustive] is applied to an enum defined in an external crate, it is considered to have multiple variants, even if only one variant is actually present.
Matching against a [range pattern][patterns.range] constitutes a discriminant read, even if
335
-
the range matches all possible values.
331
+
Matching against a [range pattern][patterns.range] performs a read of the place being matched, causing the closure to borrow it by `ImmBorrow`. This is the case even if the range matches all possible values.
Matching against a [slice pattern][patterns.slice] constitutes a discriminant read if
348
-
the slice pattern needs to inspect the length of the scrutinee.
343
+
Matching against a [slice pattern][patterns.slice] performs a read if the slice pattern needs to inspect the length of the scrutinee. The read will cause the closure to borrow the relevant place by `ImmBorrow`.
349
344
350
345
```rust,compile_fail,E0506
351
-
let mut x: &mut [i32] = &mut [1, 2, 3];
346
+
let x: &mut [i32] = &mut [1, 2, 3];
352
347
let c = || match x { // captures `*x` by ImmBorrow
353
348
[_, _, _] => println!("three elements"),
354
349
_ => println!("something else"),
@@ -357,7 +352,7 @@ x[0] += 1; // ERROR: cannot assign to `x[_]` because it is borrowed
357
352
c();
358
353
```
359
354
360
-
Thus, matching against an array doesn't constitute a discriminant read, as the length is fixed.
355
+
As such, matching against an array doesn't itself cause any borrows, as the lengthh is fixed and doesn't need to be read.
361
356
362
357
```rust
363
358
letmutx: [i32; 3] = [1, 2, 3];
@@ -368,10 +363,10 @@ x[0] += 1; // `x` can be modified while the closure is live
368
363
c();
369
364
```
370
365
371
-
Likewise, a slice pattern that matches slices of all possible lengths does not constitute a discriminant read.
366
+
Likewise, a slice pattern that matches slices of all possible lengths does not constitute a read.
0 commit comments