Copy
Like Clone, but indicates the type is can be bitwise copied.
Derivable: ✅
// Copyright 2025 Google LLC // SPDX-License-Identifier: Apache-2.0 #[derive(Debug, Clone, Copy)] pub struct Copyable(u8, u16, u32, u64); fn main() { let copyable = Copyable(1, 2, 3, 4); let copy = copyable; // Implicit copy operation dbg!(copyable); dbg!(copy); }
-
Clonerepresents an explicit, user-defined copy operation.Copyrepresents an implicit, bitwise copy. -
Should generally only be implemented on “plain data” types that should act like primitive values. For example, primitive numeric types for a linear algebra library.
-
Has the same caveat as
Clone: If duplicating the values would break an invariant, the type shouldn’t implementCopy. -
Always derive
CloneandCopytogether! Do not manually implementClonewhen implementingCopy.- Copy operations do not invoke the
clonemethod, so a customCloneimpl can have different behavior than an implicit copy operation. Deriving bothCloneandCopytogether ensures that callingclonewill give the same result as invoking a copy.
- Copy operations do not invoke the
-
Cannot be implemented on types with
Dropor non-Copyfields.-
Ask the class: Why can’t a type with heap data (
Vec,BTreeMap,Rc, etc.) beCopy?Bitwise copying on these types would mean types with heap data would no longer have exclusive ownership of a pointer, breaking the invariants usually upheld by Rust and its ecosystem.
Multiple
Vecs would point to the same data in memory. Adding and removing data would only update individualVecs length and capacity values. The same forBTreeMap.Bitwise copying of
Rcs would not update the reference counting value within the pointers, meaning there could be two instances of aRcvalue that believe themselves to be the onlyRcfor that pointer. Once one of them is destroyed, the reference count will become 0 on one of them and the inner value dropped despite there being anotherRcstill alive.
-