Skip to content

Commit 5fb1c02

Browse files
roxeloehuss
authored andcommitted
Address comments
1 parent 6d68a7f commit 5fb1c02

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
@@ -81,14 +81,15 @@ In the case where a path and one of the ancestor’s of that path are both captu
8181

8282
Note that this might need to be applied recursively.
8383

84-
```rust=
85-
let s = String::new("S");
86-
let t = (s, String::new("T"));
87-
let mut u = (t, String::new("U"));
84+
```rust
85+
# fn move_value<T>(_: T){}
86+
let s = String::from("S");
87+
let t = (s, String::from("T"));
88+
let mut u = (t, String::from("U"));
8889

8990
let c = || {
9091
println!("{:?}", u); // u captured by ImmBorrow
91-
u.0.truncate(0); // u.0 captured by MutBorrow
92+
u.1.truncate(0); // u.0 captured by MutBorrow
9293
move_value(u.0.0); // u.0.0 captured by ByValue
9394
};
9495
```
@@ -111,7 +112,7 @@ let c = || match x {
111112

112113
### Capturing references in move contexts
113114

114-
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.
115+
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.
115116

116117
```rust
117118
struct T(String, String);
@@ -122,7 +123,7 @@ let c = move || t.0.truncate(0); // closure captures (&mut t.0)
122123
```
123124

124125
### Raw pointer dereference
125-
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.
126+
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.
126127

127128
```rust,
128129
struct T(String, String);
@@ -137,7 +138,7 @@ let c = || unsafe {
137138

138139
### Reference into unaligned `struct`s
139140

140-
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.
141+
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.
141142

142143
```rust
143144
#[repr(packed)]
@@ -152,10 +153,13 @@ let c = || unsafe {
152153

153154
### `Box` vs other `Deref` implementations
154155

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

157158
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`.
158159

160+
[`Box`]: ../special-types-and-traits.md#boxt
161+
[`Deref`]: ../special-types-and-traits.md#deref-and-derefmut
162+
159163
#### Non `move` closure
160164

161165
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.
@@ -352,7 +356,7 @@ f(Closure { rect: rect });
352356
## Capture precision difference
353357

354358
r[type.closure.capture.composite]
355-
Composite types such as structs, tuples, and enums are always captured in its intirety,
359+
Composite types such as structs, tuples, and enums are always captured in its entirety,
356360
not by individual fields. As a result, it may be necessary to borrow into a local variable in order to capture a single field:
357361

358362
```rust

0 commit comments

Comments
 (0)