Skip to content

Commit dc3a39d

Browse files
committed
Explain behavior of _
Fixes #31154
1 parent 91e8044 commit dc3a39d

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/doc/book/patterns.md

+25-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,31 @@ let (x, _, z) = coordinate();
173173
Here, we bind the first and last element of the tuple to `x` and `z`, but
174174
ignore the middle element.
175175

176-
Similarly, you can use `..` in a pattern to disregard multiple values.
176+
It’s worth noting that using `_` never binds the value in the first place,
177+
which means a value may not move:
178+
179+
```rust
180+
let tuple: (u32, String) = (5, String::from("five"));
181+
182+
// Here, tuple is moved, because the String moved:
183+
let (x, _s) = tuple;
184+
185+
// The next line would give "error: use of partially moved value: `tuple`"
186+
// println!("Tuple is: {:?}", tuple);
187+
188+
// However,
189+
190+
let tuple = (5, String::from("five"));
191+
192+
// Here, tuple is _not_ moved, as the String was never moved, and u32 is Copy:
193+
let (x, _) = tuple;
194+
195+
// That means this works:
196+
println!("Tuple is: {:?}", tuple);
197+
```
198+
199+
In a similar fashion to `_`, you can use `..` in a pattern to disregard
200+
multiple values:
177201

178202
```rust
179203
enum OptionalTuple {

0 commit comments

Comments
 (0)