Skip to content

Commit 211d2a4

Browse files
committed
Improve examples in wildcard capturing section
Some of the examples in this section did not really prove the claim they were making; let's adjust them to do this. Let's also simplify and minimize them to match the style of other recent examples we're adding and that of the ones that will follow in the section below. One particular stylistic conundrum is what to do about comments like this: // ERROR: Borrow of moved value. Normally, our stylistic convention would be to capitalize after the comma in this case and end with a period. That's what we'd do, e.g., in a similar case like this: // OK: The value can be moved here. But, of course, `rustc` doesn't capitalize and add a period to these kind of error messages, making it tempting to follow that lead. Since we already don't always use the same error messages that `rustc` does -- it's not a goal to match those -- it seems better to be internally consistent with our own documentation norms. Let's capitalize and add the period. We'll later add this to the style guide and work to align the document with this.
1 parent 79cab22 commit 211d2a4

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/types/closure.md

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,16 @@ r[type.closure.capture.precision.wildcard]
189189
r[type.closure.capture.precision.wildcard.reads]
190190
Closures only capture data that needs to be read. Binding a value with a [wildcard pattern] does not read the value, so the place is not captured.
191191

192-
```rust
193-
let x = String::from("hello");
192+
```rust,no_run
193+
struct S; // A non-`Copy` type.
194+
let x = S;
194195
let c = || {
195-
let _ = x; // x is not captured
196+
let _ = x; // Does not capture `x`.
196197
};
197-
c();
198-
199-
let c = || match x { // x is not captured
200-
_ => println!("Hello World!")
198+
let c = || match x {
199+
_ => (), // Does not capture `x`.
201200
};
201+
x; // OK: `x` can be moved here.
202202
c();
203203
```
204204

@@ -250,39 +250,36 @@ c();
250250
r[type.closure.capture.precision.wildcard.fields]
251251
Fields matched against [RestPattern] (`..`) or [StructPatternEtCetera] (also `..`) are not read, and those fields are not captured.
252252

253-
```rust
254-
let x = (String::from("a"), String::from("b"));
253+
```rust,no_run
254+
struct S; // A non-`Copy` type.
255+
let x = (S, S);
255256
let c = || {
256-
let (first, ..) = x; // captures `x.0` ByValue
257+
let (x0, ..) = x; // Captures `x.0` by `ByValue`.
257258
};
258-
// The first tuple field has been moved into the closure.
259-
// The second tuple field is still accessible.
260-
println!("{:?}", x.1);
259+
// Only the first tuple field was captured by the closure.
260+
x.1; // OK: `x.1` can be moved here.
261261
c();
262262
```
263263

264264
r[type.closure.capture.precision.wildcard.array-slice]
265265
Partial captures of arrays and slices are not supported; the entire slice or array is always captured even if used with wildcard pattern matching, indexing, or sub-slicing.
266266

267267
```rust,compile_fail,E0382
268-
#[derive(Debug)]
269-
struct Example;
270-
let x = [Example, Example];
271-
268+
struct S; // A non-`Copy` type.
269+
let mut x = [S, S];
272270
let c = || {
273-
let [first, _] = x; // captures all of `x` ByValue
271+
let [x0, _] = x; // Captures all of `x` by `ByValue`.
274272
};
275-
c();
276-
println!("{:?}", x[1]); // ERROR: borrow of moved value: `x`
273+
let _ = &mut x[1]; // ERROR: Borrow of moved value.
277274
```
278275

279276
r[type.closure.capture.precision.wildcard.initialized]
280277
Values that are matched with wildcards must still be initialized.
281278

282279
```rust,compile_fail,E0381
283-
let x: i32;
280+
let x: u8;
284281
let c = || {
285-
let _ = x; // ERROR: used binding `x` isn't initialized
282+
let _ = x; // ERROR: Binding `x` isn't initialized.
286283
};
287284
```
288285

0 commit comments

Comments
 (0)