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
automatically capture anything that you refer to from within their body.
12
13
For example, `|| a + 1` automatically captures a reference to `a` from the surrounding context.
13
14
14
-
Currently, this applies to whole structs, even when only using one field.
15
+
In Rust 2018 and before, closures capture entire variables, even if the closure only uses one field.
15
16
For example, `|| a.x + 1` captures a reference to `a` and not just `a.x`.
16
-
In some situations, this is a problem.
17
-
When a field of the struct is already borrowed (mutably) or moved out of,
18
-
the other fields can no longer be used in a closure,
19
-
since that would capture the whole struct, which is no longer available.
17
+
Capturing `a` in its entirety prevents mutation or moves from other fields of `a`, so that code like this does not compile:
20
18
21
19
```rust,ignore
22
20
let a = SomeStruct::new();
23
-
24
21
drop(a.x); // Move out of one field of the struct
25
-
26
22
println!("{}", a.y); // Ok: Still use another field of the struct
27
-
28
23
let c = || println!("{}", a.y); // Error: Tries to capture all of `a`
29
24
c();
30
25
```
31
26
32
-
Starting in Rust 2021, closures will only capture the fields that they use, eliminating common borrow check errors.
33
-
So, the above example will compile fine in Rust 2021.
27
+
Starting in Rust 2021, closures captures are more precise. Typically they will only capture the fields they use (in some cases, they might capture more than just what they use, see the Rust reference for full details). Therefore, the above example will compile fine in Rust 2021.
28
+
29
+
Disjoint capture was proposed as part of [RFC 2229](https://github.com/rust-lang/rfcs/blob/master/text/2229-capture-disjoint-fields.md) and the RFC contains details about the motivation.
30
+
31
+
## Migrating to Rust 2021
34
32
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.
33
+
Changing the variables captured by a closure can cause programs to change behavior or to stop compiling in two cases:
36
34
37
-
## Changes to semantics because of disjoint capture
38
-
Disjoint captures introduces a 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.
35
+
- changes to drop order, or when destructors run ([details](#drop-order));
36
+
-changes to which traits a closure implements ([details](#trait-implementations)).
39
37
40
38
When running `cargo fix --edition`, Cargo will update the closures in your code to help you migrate to Rust 2021, as described below in [Migrations](#migrations).
39
+
40
+
Whenever any of the scenarios below are detected, `cargo fix` will insert a "dummy let" into your closure to force it to capture the entire variable:
41
+
42
+
```rust
43
+
letx= (vec![], vec![]);
44
+
letc=move|| {
45
+
// "Dummy let" that forces `x` to be captured in its entirety
46
+
let_=&x;
47
+
48
+
// Otherwise, only `x.0` would be captured here
49
+
println!("{:?}", x.0);
50
+
};
51
+
```
52
+
53
+
This is a conservative analysis: in many cases, these dummy lets can be safely removed and your program will work fine.
54
+
41
55
### Wild Card Patterns
42
-
Closures now only capture data that needs to be read, which means the following closures will not capture `x`
56
+
57
+
Closures now only capture data that needs to be read, which means the following closures will not capture `x`:
43
58
44
59
```rust
45
60
letx=10;
46
61
letc=|| {
47
-
let_=x;
62
+
let_=x;// no-op
48
63
};
49
64
50
65
letc=||matchx {
51
66
_=>println!("Hello World!")
52
67
};
53
68
```
54
69
70
+
The `let _ = x` statement here is a no-op, since the `_` pattern completely ignores the right-hand side, and `x` is a reference to a place in memory (in this case, a variable).
71
+
72
+
This change by itself (capturing fewer values) doesn't trigger any suggestions, but it may do so in conjunction with the "drop order" change below.
73
+
74
+
**Subtle:** There are other similar expressions, such as the "dummy lets" `let _ = &x` that we insert, which are not no-ops. This is because the right-hand side (`&x`) is not a reference to a place in memory, but rather an expression that must first be evaluated (and whose result is then discarded).
75
+
55
76
### Drop Order
56
-
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.
77
+
78
+
When a closure takes ownership of a value from a variable `t`, that value is then dropped when the closure is dropped, and not when the variable `t` goes out of scope:
79
+
80
+
```rust
81
+
# fnmove_value<T>(_:T){}
82
+
{
83
+
lett= (vec![0], vec![0]);
84
+
85
+
{
86
+
letc=||move_value(t); // t is moved here
87
+
} // c is dropped, which drops the tuple `t` as well
88
+
} // t goes out of scope here
89
+
```
90
+
91
+
The above code will run the same in both Rust 2018 and Rust 2021. However, in cases where the closure only takes ownership of _part_ of a variable, there can be differences:
57
92
58
93
```rust
59
94
# fnmove_value<T>(_:T){}
@@ -62,22 +97,47 @@ Since only a part of a variable might be captured instead of the entire variable
62
97
63
98
{
64
99
letc=|| {
65
-
move_value(t.0); // t.0 is moved here
100
+
// In Rust 2018, captures all of `t`.
101
+
// In Rust 2021, captures only `t.0`
102
+
move_value(t.0);
66
103
};
67
-
} // c and t.0 dropped here
68
-
} // t.1 dropped here
69
104
105
+
// In Rust 2018, `c` (and `t`) are both dropped when we
106
+
// exit this block.
107
+
//
108
+
// In Rust 2021, `c` and `t.0` are both dropped when we
109
+
// exit this block.
110
+
}
111
+
112
+
// In Rust 2018, the value from `t` has been moved and is
113
+
// not dropped.
114
+
//
115
+
// In Rust 2021, the value from `t.0` has been moved, but `t.1`
116
+
// remains, so it will be dropped here.
117
+
}
70
118
```
71
119
120
+
In most cases, dropping values at different times just affects when memory is freed and is not important. However, some `Drop` impls (aka, destructors) have side-effects, and changing the drop order in those cases can alter the semantics of your program. In such cases, the compiler will suggest inserting a dummy `let` to force the entire variable to be captured.
72
121
73
-
### Auto Traits
74
-
Structs or tuples that implement auto traits and are passed along in a closure may no longer guarantee that the closure can also implement those auto traits.
122
+
### Trait implementations
123
+
124
+
Closures automatically implement the following traits based on what values they capture:
125
+
126
+
-[`Clone`]: if all captured values are [`Clone`].
127
+
-[Auto traits] like [`Send`], [`Sync`], and [`UnwindSafe`]: if all captured values implement the given trait.
In Rust 2021, since different values are being captured, this can affect what traits a closure will implement. The migration lints test each closure to see whether it would have implemented a given trait before and whether it still implements it now; if they find that a trait used to be implemented but no longer is, then "dummy lets" are inserted.
75
136
76
137
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.
77
138
78
139
With disjoint captures, only the specific field mentioned in the closure gets captured, which wasn't originally `Send`/`Sync` defeating the purpose of the wrapper.
79
140
80
-
81
141
```rust
82
142
usestd::thread;
83
143
@@ -94,15 +154,3 @@ let c = thread::spawn(move || {
94
154
}
95
155
}); // Closure captured px.0 which is not Send
96
156
```
97
-
98
-
## Migrations
99
-
100
-
This new behavior is only activated starting in the 2021 edition,
101
-
since it can change the order in which fields are dropped and can impact auto trait usage with closures.
102
-
103
-
When running `cargo fix --edition`, the closures in your code may be updated so that they will retain the old behavior on both the 2018 and 2021 editions.
104
-
It does this by enabling the `disjoint_capture_migration` lint which adds statements like `let _ = &a;` inside the closure to force the entire variable to be captured as before.
105
-
106
-
After migrating, it is recommended to inspect the changes and see if they are necessary.
107
-
After changing the edition to 2021, you can try to remove the new statements and test that the closure works as expected.
108
-
You should review these closures, and ensure that the changes described above will not cause any problems.
0 commit comments