Skip to content

Commit ba419c7

Browse files
committed
Add move_ref_pattern docs
1 parent d27d801 commit ba419c7

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
@@ -276,6 +276,25 @@ Mutable references will set the mode to `ref mut` unless the mode is already `re
276276
which case it remains `ref`. If the automatically dereferenced value is still a reference,
277277
it is dereferenced and this process repeats.
278278

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

281300
> **<sup>Syntax</sup>**\

0 commit comments

Comments
 (0)