Skip to content

Commit a294512

Browse files
committed
Add example of what's not captured w.r.t. destructuring
In this branch, we're adding a statement in the wildcard section about how destructuring tuples, structs, and single-variant enums, by itself, does not constitute a read. Let's add some examples of this to demonstrate.
1 parent c9483b0 commit a294512

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

src/types/closure.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,46 @@ c();
203203
```
204204

205205
This also includes destructuring of tuples, structs, and single-variant enums.
206+
207+
```rust,no_run
208+
struct S; // A non-`Copy` type.
209+
210+
// Destructuring tuples does not cause a read or capture.
211+
let x = (S,);
212+
let c = || {
213+
let (..) = x; // Does not capture `x`.
214+
};
215+
x; // OK: `x` can be moved here.
216+
c();
217+
218+
// Destructuring unit structs does not cause a read or capture.
219+
let x = S;
220+
let c = || {
221+
let S = x; // Does not capture `x`.
222+
};
223+
x; // OK: `x` can be moved here.
224+
c();
225+
226+
// Destructuring structs does not cause a read or capture.
227+
struct W<T>(T);
228+
let x = W(S);
229+
let c = || {
230+
let W(..) = x; // Does not capture `x`.
231+
};
232+
x; // OK: `x` can be moved here.
233+
c();
234+
235+
// Destructuring single-variant enums does not cause a read
236+
// or capture.
237+
enum E<T> { V(T) }
238+
let x = E::V(S);
239+
let c = || {
240+
let E::V(..) = x; // Does not capture `x`.
241+
};
242+
x; // OK: `x` can be moved here.
243+
c();
244+
```
245+
206246
Fields matched with the [RestPattern] or [StructPatternEtCetera] are also not considered as read, and thus those fields will not be captured.
207247
The following illustrates some of these:
208248

0 commit comments

Comments
 (0)