diff options
| author | Manish Goregaokar <manishsmail@gmail.com> | 2016-02-03 02:54:25 +0530 |
|---|---|---|
| committer | Manish Goregaokar <manishsmail@gmail.com> | 2016-02-03 02:54:25 +0530 |
| commit | 86650211a950073e4658c6816c81bf8158f8478b (patch) | |
| tree | c026db56cdf0ed281e9a9df5a69d3cecb45769a0 | |
| parent | 5540605cd6cf68b73bbab5eb401447d23b8c55d5 (diff) | |
| parent | 6c907212b446e1c39ddb6db621bb72c6d07255f6 (diff) | |
| download | rust-86650211a950073e4658c6816c81bf8158f8478b.tar.gz rust-86650211a950073e4658c6816c81bf8158f8478b.zip | |
Rollup merge of #31352 - steveklabnik:gh31154, r=nikomatsakis
Fixes #31154
| -rw-r--r-- | src/doc/book/patterns.md | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/src/doc/book/patterns.md b/src/doc/book/patterns.md index 8e9e7246e56..6fd7f4cd475 100644 --- a/src/doc/book/patterns.md +++ b/src/doc/book/patterns.md @@ -173,7 +173,39 @@ let (x, _, z) = coordinate(); Here, we bind the first and last element of the tuple to `x` and `z`, but ignore the middle element. -Similarly, you can use `..` in a pattern to disregard multiple values. +It’s worth noting that using `_` never binds the value in the first place, +which means a value may not move: + +```rust +let tuple: (u32, String) = (5, String::from("five")); + +// Here, tuple is moved, because the String moved: +let (x, _s) = tuple; + +// The next line would give "error: use of partially moved value: `tuple`" +// println!("Tuple is: {:?}", tuple); + +// However, + +let tuple = (5, String::from("five")); + +// Here, tuple is _not_ moved, as the String was never moved, and u32 is Copy: +let (x, _) = tuple; + +// That means this works: +println!("Tuple is: {:?}", tuple); +``` + +This also means that any temporary variables will be dropped at the end of the +statement: + +```rust +// Here, the String created will be dropped immediately, as it’s not bound: + +let _ = String::from(" hello ").trim(); +``` + +You can also use `..` in a pattern to disregard multiple values: ```rust enum OptionalTuple { |
