Skip to content

Commit a84f4f7

Browse files
author
Alexis Hunt
committed
Havvy's review feedback on closure types.
1 parent 82a78c9 commit a84f4f7

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

src/types.md

+27-29
Original file line numberDiff line numberDiff line change
@@ -407,16 +407,16 @@ f(Closure{s: s, t: &t});
407407
```
408408

409409
The compiler prefers to capture a closed-over variable by immutable borrow,
410-
followed by mutable borrow and finally by move (or copy, for [`Copy`] types). It
411-
will pick the first choice of these that allows the closure to compile. If the
412-
`move` keyword is used, then all captures are by move or copy, regardless of
413-
whether a borrow would work. The `move` keyword is usually used to allow the
414-
closure to outlive the captured values, such as if the closure is being returned
415-
or used to spawn a new thread.
410+
followed by mutable borrow, by copy, and finally by move. It will pick the first
411+
choice of these that allows the closure to compile. If the `move` keyword is
412+
used, then all captures are by move or copy, regardless of whether a borrow
413+
would work. The `move` keyword is usually used to allow the closure to outlive
414+
the captured values, such as if the closure is being returned or used to spawn a
415+
new thread.
416416

417-
Structs and tuples are always captured entirely, not by individual fields. It
418-
may be necessary to borrow into a local variable in order to capture a single
419-
field:
417+
Composite types such as structs, tuples, and enums are always captured entirely,
418+
not by individual fields. It may be necessary to borrow into a local variable in
419+
order to capture a single field:
420420

421421
```rust
422422
# use std::collections::HashSet;
@@ -438,7 +438,7 @@ impl SetVec {
438438

439439
If, instead, the closure were to use `self.vec` directly, then it would attempt
440440
to capture `self` by mutable reference. But since `self.set` is already
441-
borrowed to iterate over, the closure would not compile.
441+
borrowed to iterate over, the code would not compile.
442442

443443
### Call traits and coercions
444444

@@ -453,13 +453,13 @@ more specific call traits:
453453
implements `[Fn]`, indicating that it can be called by shared reference.
454454

455455
> Note that `move` closures may still implement `[Fn]` or `[FnMut]`, even
456-
> though they capture variables by move. This is because the traits
456+
> though they capture variables by move: this is because the traits
457457
> implemented by a closure type are determined by what the closure does with
458458
> captured values, not how it captures them.
459459
460-
In addition to the call traits, *non-capturing closures*---those that don't
461-
capture anything from their environment---can be coerced to function pointers
462-
(`fn`) with the matching signature.
460+
*Non-capturing closures* are closures that don't capture anything from their
461+
environment. They can be coerced to function pointers (`fn`) with the matching
462+
signature.
463463

464464
```rust
465465
let add = |x, y| x + y;
@@ -473,29 +473,27 @@ x = bo(5,7);
473473

474474
### Other traits
475475

476-
Closure types implement the following traits, if allowed to do so by the
477-
captured values:
476+
All closure types implement `[Sized]`. Additionally, closure types implement the
477+
following traits if allowed to do so by the types of the captures it stores:
478478

479-
* `[Sized]`
480-
* `[Send]`
481-
* `[Sync]`
482479
* `[Clone]`
483480
* `[Copy]`
481+
* `[Sync]`
482+
* `[Send]`
484483

485-
`[Sized]` is always implemented (local variables are all sized, so all captured
486-
values must be too). The rules for `[Send]` and `[Sync]` match those for normal
487-
struct types, while `[Clone]` and `[Copy]` behave as if [derived][derive]. For
488-
`[Clone]`, the order of cloning of the captured variables is left unspecified.
484+
The rules for `[Send]` and `[Sync]` match those for normal struct types, while
485+
`[Clone]` and `[Copy]` behave as if [derived][derive]. For `[Clone]`, the order
486+
of cloning of the captured variables is left unspecified.
489487

490488
Because captures are often by reference, the following general rules arise:
491489

492-
* All closures are `[Sized]`.
493-
* A closure is `[Sync]` if all values captured by mutable reference, move, or
494-
copy are `[Sync]`.
495-
* A closure is `[Send]` if all values captured by shared reference are `[Sync]`,
496-
and all values captured by mutable reference, move, or copy are `[Send]`.
490+
* A closure is `[Sync]` if all variables captured by mutable reference, copy, or
491+
move are `[Sync]`.
492+
* A closure is `[Send]` if all variables captured by shared reference are
493+
`[Sync]`, and all values captured by mutable reference, copy, or move are
494+
`[Send]`.
497495
* A closure is `[Clone]` or `[Copy]` if it does not capture any values by
498-
mutable reference, and if all values it captures by move or copy are `[Clone]`
496+
mutable reference, and if all values it captures by copy or move are `[Clone]`
499497
or `[Copy]`, respectively.
500498

501499
## Trait objects

0 commit comments

Comments
 (0)