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/rust-2021/disjoint-capture-in-closures.md
+67-8
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@
3
3
## Summary
4
4
5
5
-`|| a.x + 1` now captures only `a.x` instead of `a`.
6
-
- This can subtly change the drop order of things.
6
+
- This can subtly change the drop order of things, and auto traits usage with closures.
7
7
8
8
## Details
9
9
@@ -29,12 +29,71 @@ let c = || println!("{}", a.y); // Error: Tries to capture all of `a`
29
29
c();
30
30
```
31
31
32
-
Starting in Rust 2021, closures will only capture the fields that they use.
32
+
Starting in Rust 2021, closures will only capture the fields that they use, eliminating common borrow check errors.
33
33
So, the above example will compile fine in Rust 2021.
34
34
35
-
This new behavior is only activated in the new edition,
36
-
since it can change the order in which fields are dropped.
37
-
As for all edition changes, an automatic migration is available,
38
-
which will update your closures for which this matters.
39
-
It can insert `let _ = &a;` inside the closure to force the entire
40
-
struct to be captured as before.
35
+
Disjoint capture was proposed as part of [RFC 2229](https://github.com/rust-lang/rfcs/blob/master/text/2229-capture-disjoint-fields.md), and we suggest reading the RFC to better understand motivations behind the feature.
36
+
37
+
## Changes to semantics because of disjoint capture
38
+
`#[feature(capture_disjoint_fields)]` introduces (minor) breaking change to the language. This means that there might be observable changes or valid Rust 2018 code that fails to compile once you move to Rust Edition 2021. You can use `cargo fix` with the `disjoint_capture_migrations` lint to migrate your existing code to Rust 2021.
39
+
### Wild Card Patterns
40
+
Closures now only capture data that needs to be read, which means the following closures will not capture `x`
41
+
42
+
```rust
43
+
letx=10;
44
+
letc=|| {
45
+
let_=x;
46
+
};
47
+
48
+
letc=||matchx {
49
+
_=>prinln!("Hello World!")
50
+
};
51
+
```
52
+
53
+
### Drop Order
54
+
Since only a part of a variable might be captured instead of the entire variable, when different fields or elements (in case of tuple) get drop, the drop order might be affected.
55
+
56
+
```rust
57
+
{
58
+
lett= (vec![0], vec![0]);
59
+
60
+
{
61
+
letc=|| {
62
+
move(t.0);
63
+
};
64
+
} // c and t.0 dropped here
65
+
} // t.1 dropped here
66
+
67
+
```
68
+
69
+
70
+
### Auto Traits
71
+
Structs or tuples that implement (an) auto trait(s) and are passed along in a closure may no longer guarantee that the closure can also implement that/those auto trait(s).
72
+
73
+
For instance, a common way to allow passing around raw pointers between threads is to wrap them in a struct and then implement `Send`/`Sync` auto trait for the wrapper. The closure that is passed to `thread::spawn` uses the specific fields within the wrapper but the entire wrapper is captured regardless. Since the wrapper is `Send`/`Sync`, the code is considered safe and therefore compiles successfully.
74
+
75
+
With this feature only the specific field mentioned in the closure gets captured, which wasn't originally `Send`/`Sync` defeating the purpose of the wrapper.
76
+
77
+
78
+
```rust
79
+
structPtr(*muti32);
80
+
unsafeimplSendforPtr;
81
+
82
+
83
+
letmutx=5;
84
+
letpx= (&mutxas*muti32);
85
+
86
+
letc=thread::spawn(move|| {
87
+
*(px.0) +=10;
88
+
}); // Closure captured px.0 which is not Send
89
+
```
90
+
91
+
## Migrations
92
+
93
+
This new behavior is only activated in the new edition, or by specifically enabling it using `#[feature(capture_disjoint_fields)]`,
94
+
since it can change the order in which fields are dropped and can impact auto trait usage with closures.
95
+
96
+
As for all edition changes, an automatic migration is available.
97
+
If you would like to be warned of semantics change that may impact your code, you can [use the lint](https://doc.rust-lang.org/rustc/lints/levels.html)`disjoint_capture_migrations`. The lint is also supported with [cargo fix](https://doc.rust-lang.org/cargo/commands/cargo-fix.html) to automatically migrate your code.
98
+
99
+
The migration fix involves adding `let _ = &a;` inside the closure to force the entire variable to be captured as before.
0 commit comments