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