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

Improve description of access to Struct instances when a field is moved #4207 #4208

Closed
wants to merge 2 commits into from
Closed
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
23 changes: 12 additions & 11 deletions src/ch05-01-defining-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,17 +138,18 @@ corresponding fields in `user1`, but we can choose to specify values for as
many fields as we want in any order, regardless of the order of the fields in
the struct’s definition.

Note that the struct update syntax uses `=` like an assignment; this is because
it moves the data, just as we saw in the [“Variables and Data Interacting with
Move”][move]<!-- ignore --> section. In this example, we can no longer use
`user1` as a whole after creating `user2` because the `String` in the
`username` field of `user1` was moved into `user2`. If we had given `user2` new
`String` values for both `email` and `username`, and thus only used the
`active` and `sign_in_count` values from `user1`, then `user1` would still be
valid after creating `user2`. Both `active` and `sign_in_count` are types that
implement the `Copy` trait, so the behavior we discussed in the [“Stack-Only
Data: Copy”][copy]<!-- ignore --> section would apply. We can still use
`user1.email` in this example, since its value was _not_ moved out.
Note that the struct update syntax uses `=` like an assignment, so it handles
copies and moves just as we saw in the [“Variables and Data Interacting with
Move”][move]<!-- ignore --> section. Fields that are of a type that implement
the `Copy` trait are copied from `user1` to `user2`, as discussed in [“Stack-Only
Data: Copy”][copy]<!-- ignore -->. Fields that are of a type that do not
implement the `Copy` trait are moved. In this example `user1.active` and
`user1.sign_in_count` have been copied, and so access to them is still valid.
`user1.email` has not been moved and so access to it is also still valid.
However `user1.username` has been moved and so access to it is no longer valid.
Furthermore, because one of its fields has been moved, access to `user1` directly
is also no longer valid (despite access to `user1.active`, `user1.sign_in_count`
and `user1.email` still being valid).

### Using Tuple Structs Without Named Fields to Create Different Types

Expand Down
Loading