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 partial moves example for move_ref_pattern stabilization #1377

Merged
merged 1 commit into from
Oct 19, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
- [RAII](scope/raii.md)
- [Ownership and moves](scope/move.md)
- [Mutability](scope/move/mut.md)
- [Partial moves](scope/move/partial_move.md)
- [Borrowing](scope/borrow.md)
- [Mutability](scope/borrow/mut.md)
- [Aliasing](scope/borrow/alias.md)
Expand Down
40 changes: 40 additions & 0 deletions src/scope/move/partial_move.md
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;
Copy link
Member

Choose a reason for hiding this comment

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

---- /tmp/mdbook-f6BYlI/scope/move/partial_move.md - Partial_moves (line 10) stdout ----
error[E0658]: binding by-move and by-ref in the same pattern is unstable
  --> /tmp/mdbook-f6BYlI/scope/move/partial_move.md:24:18
   |
15 |     let Person { name, ref age } = person;
   |                  ^^^^  ------- by-ref pattern here
   |                  |
   |                  by-move pattern here
   |
   = note: see issue #68354 <https://github.com/rust-lang/rust/issues/68354> for more information
   = help: add `#![feature(move_ref_pattern)]` to the crate attributes to enable```

Copy link
Contributor Author

Choose a reason for hiding this comment

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

waiting for stabilization (rust-lang/rust#76119)


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