Skip to content

Commit

Permalink
Discuss how struct update syntax moves
Browse files Browse the repository at this point in the history
Fixes #2789.
  • Loading branch information
carols10cents committed Aug 2, 2021
1 parent 70eaf49 commit 794d33a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// ANCHOR: here
let user2 = User {
active: user1.active,
username: String::from("anotherusername567"),
username: user1.username,
email: String::from("another@example.com"),
sign_in_count: user1.sign_in_count,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ fn main() {

// ANCHOR: here
let user2 = User {
username: String::from("anotherusername567"),
email: String::from("another@example.com"),
..user1
};
Expand Down
32 changes: 23 additions & 9 deletions src/ch05-01-defining-structs.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,18 @@ than `email: email`.
### Creating Instances From Other Instances With Struct Update Syntax

It’s often useful to create a new instance of a struct that uses most of an old
instance’s values but changes some. You’ll do this using *struct update syntax*.
instance’s values but changes some. You can do this using *struct update
syntax*.

First, Listing 5-6 shows how we create a new `User` instance in `user2` without
the update syntax. We set new values for `email` and `username` but otherwise
use the same values from `user1` that we created in Listing 5-2.
the update syntax. We set a new value for `email` but otherwise use the same
values from `user1` that we created in Listing 5-2.

```rust
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-06/src/main.rs:here}}
```

<span class="caption">Listing 5-6: Creating a new `User` instance using some of
<span class="caption">Listing 5-6: Creating a new `User` instance using one of
the values from `user1`</span>

Using struct update syntax, we can achieve the same effect with less code, as
Expand All @@ -115,18 +116,29 @@ explicitly set should have the same value as the fields in the given instance.
{{#rustdoc_include ../listings/ch05-using-structs-to-structure-related-data/listing-05-07/src/main.rs:here}}
```

<span class="caption">Listing 5-7: Using struct update syntax to set new
`email` and `username` values for a `User` instance but use the rest of the
values from the fields of the instance in the `user1` variable</span>
<span class="caption">Listing 5-7: Using struct update syntax to set a new
`email` value for a `User` instance but use the rest of the values from
`user1`</span>

The code in Listing 5-7 also creates an instance in `user2` that has a
different value for `email` and `username` but has the same values for the
`active` and `sign_in_count` fields from `user1`. The `..user1` must come last
different value for `email` but has the same values for the `username`,
`active`, and `sign_in_count` fields from `user1`. The `..user1` must come last
to specify that any remaining fields should get their values from the
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 is like assignment with `=` because it moves
the data, just as we saw in the [“Ways Variables and Data Interact: Move”
section][move]<!-- ignore -->. In this example, we can no longer use `user1`
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`.
The types of `active` and `sign_in_count` are types that implement the `Copy`
trait, so the behavior we discussed in the [“Stack-Only Data: Copy”
section][copy]<!-- ignore --> would apply.

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

You can also define structs that look similar to tuples, called *tuple
Expand Down Expand Up @@ -262,3 +274,5 @@ paste above
add `> ` before every line -->
[tuples]: ch03-02-data-types.html#the-tuple-type
[move]: ch04-01-what-is-ownership.html#ways-variables-and-data-interact-move
[copy]: ch04-01-what-is-ownership.html#stack-only-data-copy

0 comments on commit 794d33a

Please sign in to comment.