What Is the Copy Trait and Which Types Implement It?

The Copy trait allows types like integers and booleans to be duplicated by value rather than moved, enabling multiple owners of the same data.

The Copy trait indicates that a type can be duplicated by simply copying its bits, meaning assignment does not move ownership. Primitive types like integers (i32, u64), floating-point numbers (f32, f64), booleans (bool), and characters (char) implement Copy by default, as do tuples and structs composed entirely of Copy types. You can derive this trait for your own structs using #[derive(Copy, Clone)] if all fields are also Copy.

#[derive(Copy, Clone)]
struct Point {
    x: i32,
    y: i32,
}

fn main() {
    let p1 = Point { x: 1, y: 2 };
    let p2 = p1; // p1 is copied, not moved; both p1 and p2 are valid
    println!("p1: {:?}, p2: {:?}", p1, p2);
}