Skip to content

Commit 39a51cf

Browse files
roxeloehuss
authored andcommitted
Address comments
1 parent 5b058d5 commit 39a51cf

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/types/closure.md

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,15 @@ In the case where a path and one of the ancestor’s of that path are both captu
7676

7777
Note that this might need to be applied recursively.
7878

79-
```rust=
80-
let s = String::new("S");
81-
let t = (s, String::new("T"));
82-
let mut u = (t, String::new("U"));
79+
```rust
80+
# fn move_value<T>(_: T){}
81+
let s = String::from("S");
82+
let t = (s, String::from("T"));
83+
let mut u = (t, String::from("U"));
8384

8485
let c = || {
8586
println!("{:?}", u); // u captured by ImmBorrow
86-
u.0.truncate(0); // u.0 captured by MutBorrow
87+
u.1.truncate(0); // u.0 captured by MutBorrow
8788
move_value(u.0.0); // u.0.0 captured by ByValue
8889
};
8990
```
@@ -106,7 +107,7 @@ let c = || match x {
106107

107108
### Capturing references in move contexts
108109

109-
Rust doesn't allow moving fields out of references. As a result, in the case of move closures, when values accessed through a shared references are moved into the closure body, the compiler, instead of moving the values out of the reference, would reborrow the data.
110+
Moving fields out of references is not allowed. As a result, in the case of move closures, when values accessed through a shared references are moved into the closure body, the compiler, instead of moving the values out of the reference, would reborrow the data.
110111

111112
```rust
112113
struct T(String, String);
@@ -117,7 +118,7 @@ let c = move || t.0.truncate(0); // closure captures (&mut t.0)
117118
```
118119

119120
### Raw pointer dereference
120-
In Rust, it's `unsafe` to dereference a raw pointer. Therefore, closures will only capture the prefix of a path that runs up to, but not including, the first dereference of a raw pointer.
121+
Because it is `unsafe` to dereference a raw pointer, closures will only capture the prefix of a path that runs up to, but not including, the first dereference of a raw pointer.
121122

122123
```rust,
123124
struct T(String, String);
@@ -132,7 +133,7 @@ let c = || unsafe {
132133

133134
### Reference into unaligned `struct`s
134135

135-
In Rust, it's `unsafe` to hold references to unaligned fields in a structure, and therefore, closures will only capture the prefix of the path that runs up to, but not including, the first field access into an unaligned structure.
136+
Because it is `unsafe` to hold references to unaligned fields in a structure, closures will only capture the prefix of the path that runs up to, but not including, the first field access into an unaligned structure.
136137

137138
```rust
138139
#[repr(packed)]
@@ -147,10 +148,13 @@ let c = || unsafe {
147148

148149
### `Box` vs other `Deref` implementations
149150

150-
The compiler treats the implementation of the Deref trait for `Box` differently, as it is considered a special entity.
151+
The implementation of the [`Deref`] trait for [`Box`] is treated differently from other `Deref` implementations, as it is considered a special entity.
151152

152153
For example, let us look at examples involving `Rc` and `Box`. The `*rc` is desugared to a call to the trait method `deref` defined on `Rc`, but since `*box` is treated differently by the compiler, the compiler is able to do precise capture on contents of the `Box`.
153154

155+
[`Box`]: ../special-types-and-traits.md#boxt
156+
[`Deref`]: ../special-types-and-traits.md#deref-and-derefmut
157+
154158
#### Non `move` closure
155159

156160
In a non `move` closure, if the contents of the `Box` are not moved into the closure body, the contents of the `Box` are precisely captured.
@@ -337,7 +341,7 @@ f(Closure { rect: rect });
337341

338342
## Capture precision difference
339343

340-
Composite types such as structs, tuples, and enums are always captured in its intirety,
344+
Composite types such as structs, tuples, and enums are always captured in its entirety,
341345
not by individual fields. As a result, it may be necessary to borrow into a local variable in order to capture a single field:
342346

343347
```rust

0 commit comments

Comments
 (0)