Copy have ‘copy semantics’, allowing the value to be
duplicated instead of moved.
Signature
Examples
WithoutCopy (move semantics):
Copy (copy semantics):
Copy have ‘copy semantics’, allowing the value to be
duplicated instead of moved.
pub trait Copy
Copy (move semantics):
#[derive(Drop)]
struct Point {
x: u128,
y: u128,
}
fn main() {
let p1 = Point { x: 5, y: 10 };
foo(p1);
foo(p1); // error: Variable was previously moved.
}
fn foo(p: Point) {}
Copy (copy semantics):
#[derive(Copy, Drop)]
struct Point {
x: u128,
y: u128,
}
fn main() {
let p1 = Point { x: 5, y: 10 };
foo(p1);
foo(p1); // works: `p1` is copied when passed to `foo`
}
fn foo(p: Point) {}
Was this page helpful?