A partial move in Rust occurs when you move a specific field out of a struct, leaving the remaining fields in a partially moved state that prevents the struct from being used as a whole. This happens because the moved field is no longer valid, and Rust enforces that a struct cannot be used if any of its parts have been moved. To avoid this, you must either move the entire struct or use references to borrow the field instead.
struct Point {
x: i32,
y: i32,
}
fn main() {
let p1 = Point { x: 10, y: 20 };
let x = p1.x; // x is copied (Copy trait)
// p1 is still usable here
}
If the field does not implement Copy, moving it invalidates the struct:
struct Data {
text: String,
num: i32,
}
fn main() {
let d1 = Data { text: String::from("hello"), num: 5 };
let text = d1.text; // Partial move: 'text' is moved out
// d1.num = 10; // ERROR: cannot access 'num' because 'd1' is partially moved
}