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-10
Original file line number
Diff line number
Diff line change
@@ -75,14 +75,15 @@ In the case where a path and one of the ancestor’s of that path are both captu
75
75
76
76
Note that this might need to be applied recursively.
77
77
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
+
# fnmove_value<T>(_:T){}
80
+
lets=String::from("S");
81
+
lett= (s, String::from("T"));
82
+
letmutu= (t, String::from("U"));
82
83
83
84
letc=|| {
84
85
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
86
87
move_value(u.0.0); // u.0.0 captured by ByValue
87
88
};
88
89
```
@@ -105,7 +106,7 @@ let c = || match x {
105
106
106
107
### Capturing references in move contexts
107
108
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.
109
110
110
111
```rust
111
112
structT(String, String);
@@ -116,7 +117,7 @@ let c = move || t.0.truncate(0); // closure captures (&mut t.0)
116
117
```
117
118
118
119
### 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.
120
121
121
122
```rust,
122
123
struct T(String, String);
@@ -131,7 +132,7 @@ let c = || unsafe {
131
132
132
133
### Reference into unaligned `struct`s
133
134
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.
135
136
136
137
```rust
137
138
#[repr(packed)]
@@ -146,10 +147,13 @@ let c = || unsafe {
146
147
147
148
### `Box` vs other `Deref` implementations
148
149
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.
150
151
151
152
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