trait

Rust Copy trait: implicit duplication

Copy marks types whose values can be implicitly duplicated through ordinary value use. It has no methods. A type must also implement Clone, and all fields must support Copy. String is not Copy; shared references are Copy even if the referenced type is not. Expected output (not executed): 4 4 Why: u32 is Copy, so quantity remains usable. Common mistake: Confusing Copy with an independent duplication of referenced data, or assuming every fixed-size type is Copy.

Formal Syntax & Structure

rust syntaxrust
pub trait Copy: Clone {}

Working Example

Example Snippetrust
fn main() {    let quantity = 4_u32;    let reserved = quantity;    println!("{quantity} {reserved}");}
Rust Reference