Skip to content

Commit e526381

Browse files
authored
Merge pull request #881 from Amjad50/move_ref_pattern
Add `move_ref_pattern` docs
2 parents 1b78182 + 840993a commit e526381

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

src/patterns.md

+19
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,25 @@ Mutable references will set the mode to `ref mut` unless the mode is already `re
284284
which case it remains `ref`. If the automatically dereferenced value is still a reference,
285285
it is dereferenced and this process repeats.
286286

287+
Move bindings and reference bindings can be mixed together in the same pattern, doing so will
288+
result in partial move of the object bound to and the object cannot be used afterwards.
289+
This applies only if the type cannot be copied.
290+
291+
In the example below, `name` is moved out of `person`, trying to use `person` as a whole or
292+
`person.name` would result in an error because of *partial move*.
293+
294+
Example:
295+
296+
```rust
297+
# struct Person {
298+
# name: String,
299+
# age: u8,
300+
# }
301+
# let person = Person{ name: String::from("John"), age: 23 };
302+
// `name` is moved from person and `age` referenced
303+
let Person { name, ref age } = person;
304+
```
305+
287306
## Wildcard pattern
288307

289308
> **<sup>Syntax</sup>**\

0 commit comments

Comments
 (0)