Skip to content

Commit fb73551

Browse files
committed
Make the examples testable
1 parent c72d0bc commit fb73551

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/rust-2021/disjoint-capture-in-closures.md

+12-7
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ When running `cargo fix --edition`, Cargo will update the closures in your code
4141
### Wild Card Patterns
4242
Closures now only capture data that needs to be read, which means the following closures will not capture `x`
4343

44-
```rust,ignore
44+
```rust
4545
let x = 10;
4646
let c = || {
4747
let _ = x;
@@ -55,13 +55,14 @@ let c = || match x {
5555
### Drop Order
5656
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.
5757

58-
```rust,ignore
58+
```rust
59+
# fn move_value<T>(_: T){}
5960
{
6061
let t = (vec![0], vec![0]);
6162

6263
{
6364
let c = || {
64-
move(t.0);
65+
move_value(t.0); // t.0 is moved here
6566
};
6667
} // c and t.0 dropped here
6768
} // t.1 dropped here
@@ -77,16 +78,20 @@ For instance, a common way to allow passing around raw pointers between threads
7778
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.
7879

7980

80-
```rust,ignore
81+
```rust
82+
use std::thread;
83+
8184
struct Ptr(*mut i32);
82-
unsafe impl Send for Ptr;
85+
unsafe impl Send for Ptr {}
8386

8487

8588
let mut x = 5;
86-
let px = (&mut x as *mut i32);
89+
let px = Ptr(&mut x as *mut i32);
8790

8891
let c = thread::spawn(move || {
89-
*(px.0) += 10;
92+
unsafe {
93+
*(px.0) += 10;
94+
}
9095
}); // Closure captured px.0 which is not Send
9196
```
9297

0 commit comments

Comments
 (0)