Skip to content

Latest commit

 

History

History
48 lines (37 loc) · 2.54 KB

File metadata and controls

48 lines (37 loc) · 2.54 KB

In Rust, both Copy and Clone are traits used for duplicating values, but they serve different purposes and have different implications:

Copy

  • Trait Definition: The Copy trait signifies that a type's values can be duplicated simply by copying bits. It's a marker trait, meaning it has no methods to implement.
  • Implicit Semantics: When a type implements Copy, its values are duplicated implicitly by the compiler. This means that when you assign a Copy type to another variable or pass it to a function, the value is copied rather than moved.
  • Requirements: Types that implement Copy must have a simple and efficient bitwise copy. This generally applies to types like scalars (integers, floats), tuples of Copy types, and arrays of Copy types.
  • Usage Example:
    let x = 5;
    let y = x; // x is still usable because i32 implements Copy

Clone

  • Trait Definition: The Clone trait is for types that can explicitly create a deep copy of their values. It includes a method, clone, which you must implement.
  • Explicit Semantics: Cloning is explicit and must be called with the .clone() method. This allows for more complex and potentially expensive operations than a simple bitwise copy.
  • Requirements: Any type can implement Clone, even if it requires heap allocation or other resource management.
  • Usage Example:
    #[derive(Clone)]
    struct MyStruct {
        data: Vec<i32>,
    }
    
    let a = MyStruct { data: vec![1, 2, 3] };
    let b = a.clone(); // `a` is still usable, and `b` is a deep copy of `a`

Key Differences

  1. Implicit vs. Explicit:

    • Copy is implicit and happens automatically.
    • Clone is explicit and requires calling .clone().
  2. Cost:

    • Copy is usually very cheap as it involves a simple bitwise copy.
    • Clone can be more expensive because it may involve deep copying of data, such as allocating new heap memory.
  3. Restrictions:

    • Only types that can be safely duplicated with a simple bitwise copy can implement Copy.
    • Any type can implement Clone, including those that manage heap memory or other resources.
  4. Ownership Semantics:

    • For Copy types, both the original and the copied value can be used independently after the copy.
    • For Clone types, the clone method must be called to explicitly duplicate the value, and the original can still be used.

In summary, use Copy for simple types where bitwise copying is sufficient and cheap, and use Clone for types that require a more complex duplication process.