On this page
Copy
Prev: str | Up: builtins | Next: Clone
Rel: builtins, Ownership, Borrowing & Lifetimes
Ref: Rust Docs — Copy
Tags: #public #prog #rust #builtins #tier/core
Availability
Built-in / Prelude (Marker Trait) — available with zero use imports.
Syntax & Overview
#[derive(Copy, Clone)] struct Point { x: f64, y: f64 }
Marks stack-only bitwise-copyable types so assignment copies instead of moving ownership.
Notes & Pitfalls
- Implicit bitwise copy on assignment: Values implementing
Copyare never moved; both the old and new bindings stay valid. - Requires
Cloneand forbidsDrop: Types managing heap memory (String,Vec) cannot beCopy; only plain-data types (i32,bool,&T, small POD structs) can derive#[derive(Copy, Clone)].
Example
#[derive(Copy, Clone, Debug, PartialEq)]
struct Point { x: i32, y: i32 }
fn main() {
let p1 = Point { x: 3, y: 4 };
let p2 = p1; // bitwise copied, not moved
assert_eq!(p1.x + p2.y, 7);
}
Linked from
Related notes
- tuts/rust/builtins/Clone Linked here; links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/str Linked here; links to this page; shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins Linked here; links to this page; shared tags: #builtins, #prog, #rust
- tuts/rust/ownership_and_internals/ownership_borrowing_and_lifetimes Linked here; links to this page; shared tags: #prog, #rust
- tuts/rust/builtins/Drop shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/From shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Iterator shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/macro_rules shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/match shared tags: #builtins, #prog, #rust, #tier
- tuts/rust/builtins/Option shared tags: #builtins, #prog, #rust, #tier