From 88c3e558feb5d7e9d81e294a217e47611de0f124 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Sampaio?= Date: Sun, 29 Sep 2024 11:01:12 +0100 Subject: [PATCH 1/3] Mention move of individual struct fields --- src/ch05-01-defining-structs.md | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/ch05-01-defining-structs.md b/src/ch05-01-defining-structs.md index c5db4dc74d..1cf57d1db9 100644 --- a/src/ch05-01-defining-structs.md +++ b/src/ch05-01-defining-structs.md @@ -147,14 +147,18 @@ 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] 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] section would apply. +Move”][move] section. However, the fields are moved +individually, and the original struct is not invalidated if it contains +reference the data that wasn't moved. + +In Listing 5-7, we can no longer use `user1.username` because it was moved into +`user2.username`. However, we can continue to use `user1.email` normally. 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.username` 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] section would +apply. ### Using Tuple Structs Without Named Fields to Create Different Types From b82de7ecd52325151bdb687fbafb529ab8161b5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Sampaio?= Date: Tue, 1 Oct 2024 19:07:52 +0100 Subject: [PATCH 2/3] Revert "Mention move of individual struct fields" This reverts commit 88c3e558feb5d7e9d81e294a217e47611de0f124. --- src/ch05-01-defining-structs.md | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/ch05-01-defining-structs.md b/src/ch05-01-defining-structs.md index 1cf57d1db9..c5db4dc74d 100644 --- a/src/ch05-01-defining-structs.md +++ b/src/ch05-01-defining-structs.md @@ -147,18 +147,14 @@ 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] section. However, the fields are moved -individually, and the original struct is not invalidated if it contains -reference the data that wasn't moved. - -In Listing 5-7, we can no longer use `user1.username` because it was moved into -`user2.username`. However, we can continue to use `user1.email` normally. 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.username` 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] section would -apply. +Move”][move] 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] section would apply. ### Using Tuple Structs Without Named Fields to Create Different Types From 26bea114edb01746afbab284fdc97330f61aa2b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Sampaio?= Date: Tue, 1 Oct 2024 19:13:03 +0100 Subject: [PATCH 3/3] Add note about field access after struct update syntax --- src/ch05-01-defining-structs.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/ch05-01-defining-structs.md b/src/ch05-01-defining-structs.md index c5db4dc74d..f84785cde1 100644 --- a/src/ch05-01-defining-structs.md +++ b/src/ch05-01-defining-structs.md @@ -154,7 +154,8 @@ Move”][move] section. In this example, we can no longer use `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] section would apply. +Data: Copy”][copy] section would apply. We can still use +`user1.email` in this example, since its value was _not_ moved out. ### Using Tuple Structs Without Named Fields to Create Different Types