Skip to content
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

Add move_ref_pattern docs #881

Merged
merged 1 commit into from
Oct 17, 2020
Merged
Changes from all commits
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
19 changes: 19 additions & 0 deletions src/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,25 @@ Mutable references will set the mode to `ref mut` unless the mode is already `re
which case it remains `ref`. If the automatically dereferenced value is still a reference,
it is dereferenced and this process repeats.

Move bindings and reference bindings can be mixed together in the same pattern, doing so will
result in partial move of the object bound to and the object cannot be used afterwards.
This applies only if the type cannot be copied.

In the example below, `name` is moved out of `person`, trying to use `person` as a whole or
`person.name` would result in an error because of *partial move*.

Example:

```rust
# struct Person {
# name: String,
# age: u8,
# }
# let person = Person{ name: String::from("John"), age: 23 };
// `name` is moved from person and `age` referenced
let Person { name, ref age } = person;
```

## Wildcard pattern

> **<sup>Syntax</sup>**\
Expand Down