21.19 Partial Moves in Patterns (Advanced)

In Rust, partial moves allow you to move some fields from a value while still borrowing others, all in a single pattern. This is an advanced topic, but it can be very useful when dealing with large structs or data you only want to partially transfer ownership of. For example:

struct Data {
    info: String,
    count: i32,
}

fn process(data: Data) {
    // Suppose we only want to move `info` out, but reference `count`
    let Data { info, ref count } = data;
    
    println!("info was moved and is now owned here: {}", info);
    // We can still use data.count through `count`, which is a reference
    println!("count is accessible by reference: {}", count);
    // data is partially moved, so we can't use data.info here anymore, 
    // but we can read data.count if needed.
}

This pattern extracts ownership of data.info into the local variable info while taking a reference to data.count. Afterward, data.info is no longer available (since ownership moved), but data.count can still be accessed through count.

Partial moves can reduce cloning costs and sometimes simplify code, but they also require careful tracking of which parts of a struct remain valid and which have been moved.