-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1377 from Amjad50/move_ref_pattern
Add partial moves example for `move_ref_pattern` stabilization
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Partial moves | ||
|
||
Pattern bindings can have `by-move` and `by-reference` bindings at | ||
the same time which is used in [destructuring]. Using these pattern | ||
will result in partial move for the variable, which means that part | ||
of the variable is moved while other parts stayed. In this case, the | ||
parent variable cannot be used afterwards as a whole. However, parts | ||
of it that are referenced and not moved can be used. | ||
|
||
```rust,editable | ||
fn main() { | ||
#[derive(Debug)] | ||
struct Person { | ||
name: String, | ||
age: u8, | ||
} | ||
let person = Person { | ||
name: String::from("Alice"), | ||
age: 20, | ||
}; | ||
// `name` is moved out of person, but `age` is referenced | ||
let Person { name, ref age } = person; | ||
println!("The person's age is {}", age); | ||
println!("The person's name is {}", name); | ||
// Error! borrow of partially moved value: `person` partial move occurs | ||
//println!("The person struct is {:?}", person); | ||
// `person` cannot be used but `person.age` can be used as it is not moved | ||
println!("The person's age from person struct is {}", person.age); | ||
} | ||
``` | ||
### See also: | ||
[destructuring][destructuring] | ||
|
||
[destructuring]: ../../flow_control/match/destructuring.md |