@@ -407,16 +407,16 @@ f(Closure{s: s, t: &t});
407
407
```
408
408
409
409
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.
416
416
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:
420
420
421
421
``` rust
422
422
# use std :: collections :: HashSet ;
@@ -438,7 +438,7 @@ impl SetVec {
438
438
439
439
If, instead, the closure were to use ` self.vec ` directly, then it would attempt
440
440
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.
442
442
443
443
### Call traits and coercions
444
444
@@ -453,13 +453,13 @@ more specific call traits:
453
453
implements ` [Fn] ` , indicating that it can be called by shared reference.
454
454
455
455
> 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
457
457
> implemented by a closure type are determined by what the closure does with
458
458
> captured values, not how it captures them.
459
459
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.
463
463
464
464
``` rust
465
465
let add = | x , y | x + y ;
@@ -473,29 +473,27 @@ x = bo(5,7);
473
473
474
474
### Other traits
475
475
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 :
478
478
479
- * ` [Sized] `
480
- * ` [Send] `
481
- * ` [Sync] `
482
479
* ` [Clone] `
483
480
* ` [Copy] `
481
+ * ` [Sync] `
482
+ * ` [Send] `
484
483
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.
489
487
490
488
Because captures are often by reference, the following general rules arise:
491
489
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] ` .
497
495
* 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] `
499
497
or ` [Copy] ` , respectively.
500
498
501
499
## Trait objects
0 commit comments