Skip to content

Commit 24f4e32

Browse files
authored
Merge pull request #369 from crlf0710/patch-1
[RFC 231] Capture mode and closure trait rules
2 parents f6185bb + 14345fb commit 24f4e32

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/expressions/closure-expr.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ Closure expressions are most useful when passing functions as arguments to other
2424
functions, as an abbreviation for defining and capturing a separate function.
2525

2626
Significantly, closure expressions _capture their environment_, which regular
27-
[function definitions] do not. Without the `move`
28-
keyword, the closure expression infers how it captures each variable from its
29-
environment, preferring to capture by shared reference, effectively borrowing
27+
[function definitions] do not. Without the `move` keyword, the closure expression
28+
[infers how it captures each variable from its environment](types.html#capture-modes),
29+
preferring to capture by shared reference, effectively borrowing
3030
all outer variables mentioned inside the closure's body. If needed the compiler
3131
will infer that instead mutable references should be taken, or that the values
3232
should be moved or copied (depending on their type) from the environment. A
@@ -35,7 +35,7 @@ prefixing it with the `move` keyword. This is often used to ensure that the
3535
closure's type is `'static`.
3636

3737
The compiler will determine which of the [closure
38-
traits](types.html#closure-types) the closure's type will implement by how it
38+
traits](types.html#call-traits-and-coercions) the closure's type will implement by how it
3939
acts on its captured variables. The closure will also implement
4040
[`Send`](special-types-and-traits.html#send) and/or
4141
[`Sync`](special-types-and-traits.html#sync) if all of its captured types do.

src/types.md

+2
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,8 @@ so that the call to `f` works as if it were:
414414
f(Closure{s: s, t: &t});
415415
```
416416

417+
### Capture modes
418+
417419
The compiler prefers to capture a closed-over variable by immutable borrow,
418420
followed by unique immutable borrow (see below), by mutable borrow, and finally
419421
by move. It will pick the first choice of these that allows the closure to

0 commit comments

Comments
 (0)