ownership

Rust Partial moves: transfer a field

A struct without a custom Drop implementation can have an owned non-Copy field moved out. Its remaining unmoved fields can still be used, but the entire partially moved struct cannot be read as a complete value until restored. Expected output (not executed): Tea for Sam Why: Only product moved; customer is still initialized. Common mistake: Assuming moving one field makes every other field inaccessible. A type implementing Drop restricts such field moves.

Formal Syntax & Structure

rust syntaxrust
let field = record.field;

Working Example

Example Snippetrust
struct Order {    product: String,    customer: String,}fn main() {    let order = Order { product: "Tea".into(), customer: "Sam".into() };    let product = order.product;    println!("{product} for {}", order.customer);}
Rust Reference