Common Traits

Traits that are important to understand due to their broad applicability / frequency in Rust.

Copy vs Clone

Blog post with a bit more detail. Summary:

  • Copy is a simple bitwise copy. Used for types stored on the stack. Cannot be used for types which have a Drop impl and/or are stored on the heap.
  • Clone is a more arbitrary duplication of types - so can include “deep copies”, heap-allocated types, etc.
  • All Copy types must also implement Clone, but not all Clone types are also Copy.
  • ‘By default, variable bindings have “move semantics’. However, if a type implements Copy, it instead has ‘copy semantics’.”1

  1. https://doc.rust-lang.org/std/marker/trait.Copy.html ↩︎