Start with the idea
Destructuring can move an owned field and copy or borrow the rest. After a partial move the whole struct is unavailable, but fields that were not moved may still be used directly.
A struct can be partly moved when one field owns data and another field is still independently usable. In the example, user.name is a String, so assigning it to name transfers that field's ownership. user.level is a u8, so it can still be read and copied. The whole user value can no longer be used as a complete User because one of its fields is missing. The compiler tracks that state precisely.
This is useful when consuming one payload but retaining metadata. Destructure a value and decide for each field whether to move, copy, or borrow it.
Keep learning, for free
The rest of this lesson is waiting for you.
Create a free account to read the complete explanation, see the infographic, explore the code, and try the exercises. All 742 lessons and algorithm steps are included.
No payment details. Your account opens the complete Academy.