Skip to content

Commit 8665021

Browse files
committed
Rollup merge of rust-lang#31352 - steveklabnik:gh31154, r=nikomatsakis
Fixes rust-lang#31154
2 parents 5540605 + 6c90721 commit 8665021

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/doc/book/patterns.md

+33-1
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,39 @@ 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+
This also means that any temporary variables will be dropped at the end of the
200+
statement:
201+
202+
```rust
203+
// Here, the String created will be dropped immediately, as it’s not bound:
204+
205+
let _ = String::from(" hello ").trim();
206+
```
207+
208+
You can also use `..` in a pattern to disregard multiple values:
177209

178210
```rust
179211
enum OptionalTuple {

0 commit comments

Comments
 (0)