Skip to content

Explain behavior of _ #31352

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 3, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/doc/book/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,31 @@ 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);
```

In a similar fashion to `_`, you can use `..` in a pattern to disregard
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/in a pattern/in some patterns
.. unlike _ is not an independent pattern, it's part of some other pattern's syntax

multiple values:

```rust
enum OptionalTuple {
Expand Down