Skip to content

[RFC 231] Capture mode and closure trait rules #369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/expressions/closure-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ Closure expressions are most useful when passing functions as arguments to other
functions, as an abbreviation for defining and capturing a separate function.

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

The compiler will determine which of the [closure
traits](types.html#closure-types) the closure's type will implement by how it
traits](types.html#call-traits-and-coercions) the closure's type will implement by how it
acts on its captured variables. The closure will also implement
[`Send`](special-types-and-traits.html#send) and/or
[`Sync`](special-types-and-traits.html#sync) if all of its captured types do.
Expand Down
2 changes: 2 additions & 0 deletions src/types.md
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,8 @@ so that the call to `f` works as if it were:
f(Closure{s: s, t: &t});
```

### Capture modes

The compiler prefers to capture a closed-over variable by immutable borrow,
followed by unique immutable borrow (see below), by mutable borrow, and finally
by move. It will pick the first choice of these that allows the closure to
Expand Down