Skip to content

Commit fdf136e

Browse files
author
Alexis Hunt
committed
Auto traits with unique references; better example
1 parent a0c5784 commit fdf136e

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/types.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,10 +462,13 @@ reference, as in the following example:
462462
```rust
463463
let mut b = false;
464464
let x = &mut b;
465-
let c = || { *x = true; };
466-
// The following line is an error:
467-
// let y = &x;
468-
c();
465+
{
466+
let mut c = || { *x = true; };
467+
// The following line is an error:
468+
// let y = &x;
469+
c();
470+
}
471+
let z = &x;
469472
```
470473

471474
In this case, borrowing `x` mutably is not possible, because `x` is not `mut`.
@@ -474,7 +477,8 @@ because a `& &mut` reference may not be unique, so it cannot safely be used to
474477
modify a value. So a unique immutable borrow is used: it borrows `x` immutably,
475478
but like a mutable borrow, it must be unique. In the above example, uncommenting
476479
the declaration of `y` will produce an error because it would violate the
477-
uniqueness of the closure's borrow of `x`.
480+
uniqueness of the closure's borrow of `x`; the declaration of z is valid because
481+
the closure's lifetime has expired at the end of the block, releasing the borrow.
478482

479483
### Call traits and coercions
480484

@@ -524,12 +528,12 @@ of cloning of the captured variables is left unspecified.
524528
Because captures are often by reference, the following general rules arise:
525529

526530
* A closure is [`Sync`] if all captured variables are [`Sync`].
527-
* A closure is [`Send`] if all variables captured by shared reference are
528-
[`Sync`], and all values captured by mutable reference, copy, or move are
529-
[`Send`].
531+
* A closure is [`Send`] if all variables captured by non-unique immutable
532+
reference are [`Sync`], and all values captured by unique immutable or mutable
533+
reference, copy, or move are [`Send`].
530534
* A closure is [`Clone`] or [`Copy`] if it does not capture any values by
531-
mutable reference, and if all values it captures by copy or move are
532-
[`Clone`] or [`Copy`], respectively.
535+
unique immutable or mutable reference, and if all values it captures by copy
536+
or move are [`Clone`] or [`Copy`], respectively.
533537

534538
## Trait objects
535539

0 commit comments

Comments
 (0)