ownership

Rust Derive Clone for owned records

Deriving Clone on a struct delegates cloning to its fields. The result therefore inherits each field’s duplication and sharing behavior. A struct can support Clone without supporting Copy. Expected output (not executed): Pen: 2 / 1 Why: The String field is cloned and the numeric price is copied by its Clone implementation. Common mistake: Assuming derive makes the type Copy or changes the meaning of Clone for smart-pointer fields.

Formal Syntax & Structure

rust syntaxrust
#[derive(Clone)]

Working Example

Example Snippetrust
#[derive(Clone)]struct Product { name: String, price: u32 }fn main() {    let item = Product { name: "Pen".into(), price: 2 };    let mut sale = item.clone();    sale.price = 1;    println!("{}: {} / {}", item.name, item.price, sale.price);}
Rust Reference