What is the difference between Clone and Copy

Clone creates a deep copy of data while Copy performs a shallow bitwise copy for simple types.

Clone creates a deep copy of the data, while Copy performs a shallow bitwise copy without moving ownership. Use Clone for types that manage heap memory like String, and Copy for simple types like integers that implement the Copy trait.

fn main() {
    let s1 = String::from("hello");
    let s2 = s1.clone(); // Deep copy: both s1 and s2 own separate data

    let x = 5;
    let y = x; // Shallow copy: x is copied, both x and y are valid

    println!("s1 = {s1}, s2 = {s2}");
    println!("x = {x}, y = {y}");
}