Skip to content

Commit de591f5

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

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

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

+12-8
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,13 @@ 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
5959
{
6060
let t = (vec![0], vec![0]);
6161

6262
{
63-
let c = || {
64-
move(t.0);
63+
let c = move || {
64+
t.0; // t.0 is moved here
6565
};
6666
} // c and t.0 dropped here
6767
} // t.1 dropped here
@@ -77,16 +77,20 @@ For instance, a common way to allow passing around raw pointers between threads
7777
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.
7878

7979

80-
```rust,ignore
80+
```rust
81+
use std::thread;
82+
8183
struct Ptr(*mut i32);
82-
unsafe impl Send for Ptr;
84+
unsafe impl Send for Ptr {}
8385

8486

8587
let mut x = 5;
86-
let px = (&mut x as *mut i32);
88+
let px = Ptr(&mut x as *mut i32);
8789

8890
let c = thread::spawn(move || {
89-
*(px.0) += 10;
91+
unsafe {
92+
*(px.0) += 10;
93+
}
9094
}); // Closure captured px.0 which is not Send
9195
```
9296

0 commit comments

Comments
 (0)