Skip to content

Commit 37ce76c

Browse files
committed
Address comment
1 parent 881f305 commit 37ce76c

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/types/closure.md

+14-10
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,15 @@ In the case where a path and one of the ancestor’s of that path are both captu
7575

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

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

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

106107
### Capturing references in move contexts
107108

108-
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.
109+
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.
109110

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

118119
### Raw pointer dereference
119-
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.
120+
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.
120121

121122
```rust,
122123
struct T(String, String);
@@ -131,7 +132,7 @@ let c = || unsafe {
131132

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

134-
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.
135+
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.
135136

136137
```rust
137138
#[repr(packed)]
@@ -146,10 +147,13 @@ let c = || unsafe {
146147

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

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

151152
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`.
152153

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

155159
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.
@@ -336,7 +340,7 @@ f(Closure { rect: rect });
336340

337341
## Capture precision difference
338342

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

342346
```rust

0 commit comments

Comments
 (0)