
in Rust
is a creational design pattern that allows cloning objects, even complex ones, without coupling to their specific classes.
All classes should have a common interface that makes it possible to copy objects even if their concrete classes are unknown. objects can produce full copies since objects of the same class can access each other’s private fields.
Built-in Clone trait
Rust has a built-in std::clone::Clone
trait with many implementations for various types (via #[derive(Clone)]
). Thus, the pattern is ready to use out of the box.
main.rs
#[derive(Clone)]
struct Circle {
pub x: u32,
pub y: u32,
pub radius: u32,
}
fn main() {
let circle1 = Circle {
x: 10,
y: 15,
radius: 10,
};
// in action.
let mut circle2 = circle1.clone();
circle2.radius = 77;
println!("Circle 1: {}, {}, {}", circle1.x, circle1.y, circle1.radius);
println!("Circle 2: {}, {}, {}", circle2.x, circle2.y, circle2.radius);
}
Output
Circle 1: 10, 15, 10 Circle 2: 10, 15, 77