
Understand it
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. A pattern such as let User { name, ref level } = user; moves name and borrows level. If you need the original struct intact, borrow the name instead of moving it, or clone only that field when an independent owned copy is truly needed. Cloning the entire struct is often wasteful and hides the intended ownership transfer.
Try printing name and user.level, then attempt println!("{user:?}") after the move and inspect the error. Rebuild a new User from the moved name and copied level to see how ownership can be reassembled. Types implementing Drop cannot have individual fields moved out this way, because their destructor expects a complete value. In those cases, consider replacing a field through an Option or using an API that consumes the whole object deliberately.
With larger structs, destructuring at the boundary of a function can make the intended field-level ownership transfer clearer than scattered accesses throughout the body.
Read the code
#[derive(Debug)]
struct User {
name: String,
level: u8,
}
let user = User {
name: "Ada".into(), level: 7,
};
let name = user.name;
println!("{}", user.level);The example makes the contract visible in code.
Try it
Predict which of user.name, user.level, and user stay usable after let name = user.name.
What success looks like: You can explain why user.level still works and why the whole user does not.
Check your understanding
Why is user.name no longer available?
Reveal the answer
- It was moved
- level borrowed it
- Structs expire
Correct: It was moved.
Go deeper
Common misconception: “After let name = user.name, all of user is gone.”
Only the name field moved. user.level is still readable, but user as a whole is not, so println!("{user:?}") gets E0382.
- Check each field:
user.nameis aString, solet name = user.name;moves it out.user.levelis au8and was never moved. - Predict before checking: After the move, predict whether
println!("{}", user.level);andprintln!("{user:?}");each compile. - Compare with evidence: The first prints
7. The second gets E0382, a borrow of the partially moveduser.
Transfer challenge: Rewrite the move as let User { name, ref level } = user; and print both. Then add impl Drop for User and read the error on moving user.name out.
Connections
- Reborrowing: Lend access without consuming the original reference
- Interior Mutability: Move a checked borrow rule to runtime
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Partial Moves (courseId "ownership", lessonId "partial-moves") Link: https://crabrix.com/learn/ownership/partial-moves/ Goal: Destructuring can move an owned field and copy or borrow the rest. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=ownership/partial-moves) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.