You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/types/closure.md
+14-10Lines changed: 14 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -81,14 +81,15 @@ In the case where a path and one of the ancestor’s of that path are both captu
81
81
82
82
Note that this might need to be applied recursively.
83
83
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
+
# fnmove_value<T>(_:T){}
86
+
lets=String::from("S");
87
+
lett= (s, String::from("T"));
88
+
letmutu= (t, String::from("U"));
88
89
89
90
letc=|| {
90
91
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
92
93
move_value(u.0.0); // u.0.0 captured by ByValue
93
94
};
94
95
```
@@ -111,7 +112,7 @@ let c = || match x {
111
112
112
113
### Capturing references in move contexts
113
114
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.
115
116
116
117
```rust
117
118
structT(String, String);
@@ -122,7 +123,7 @@ let c = move || t.0.truncate(0); // closure captures (&mut t.0)
122
123
```
123
124
124
125
### 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.
126
127
127
128
```rust,
128
129
struct T(String, String);
@@ -137,7 +138,7 @@ let c = || unsafe {
137
138
138
139
### Reference into unaligned `struct`s
139
140
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.
141
142
142
143
```rust
143
144
#[repr(packed)]
@@ -152,10 +153,13 @@ let c = || unsafe {
152
153
153
154
### `Box` vs other `Deref` implementations
154
155
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.
156
157
157
158
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`.
0 commit comments