diff --git a/src/patterns/creational/prototype.md b/src/patterns/creational/prototype.md index 9edae74..2490d43 100644 --- a/src/patterns/creational/prototype.md +++ b/src/patterns/creational/prototype.md @@ -5,29 +5,7 @@ The prototype pattern is a creational pattern that allows cloning of objects, ev We can implement the pattern using the `Clone` trait. Here is an example: ```rust -#[derive(Clone)] -struct Prototype { - field1: String, - field2: i32, -} - -impl Prototype { - fn new(field1: String, field2: i32) -> Self { - Prototype { field1, field2 } - } - - fn clone_prototype(&self) -> Self { - self.clone() - } -} - -fn main() { - let original = Prototype::new(String::from("Prototype"), 42); - let cloned = original.clone_prototype(); - - println!("Original: {} - {}", original.field1, original.field2); - println!("Cloned: {} - {}", cloned.field1, cloned.field2); -} +{{#include prototype/src/main.rs}} ``` - We define a `Prototype` struct with two fields. diff --git a/src/patterns/creational/prototype/Cargo.lock b/src/patterns/creational/prototype/Cargo.lock new file mode 100644 index 0000000..dde5f9d --- /dev/null +++ b/src/patterns/creational/prototype/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "prototype_pattern" +version = "0.1.0" diff --git a/src/patterns/creational/prototype/Cargo.toml b/src/patterns/creational/prototype/Cargo.toml new file mode 100644 index 0000000..8965b02 --- /dev/null +++ b/src/patterns/creational/prototype/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "prototype_pattern" +version = "0.1.0" +edition = "2018" + +[dependencies] \ No newline at end of file diff --git a/src/patterns/creational/prototype/src/main.rs b/src/patterns/creational/prototype/src/main.rs new file mode 100644 index 0000000..cae6155 --- /dev/null +++ b/src/patterns/creational/prototype/src/main.rs @@ -0,0 +1,23 @@ +#[derive(Clone)] +struct Prototype { + field1: String, + field2: i32, +} + +impl Prototype { + fn new(field1: String, field2: i32) -> Self { + Prototype { field1, field2 } + } + + fn clone_prototype(&self) -> Self { + self.clone() + } +} + +fn main() { + let original = Prototype::new(String::from("Prototype"), 42); + let cloned = original.clone_prototype(); + + println!("Original: {} - {}", original.field1, original.field2); + println!("Cloned: {} - {}", cloned.field1, cloned.field2); +} \ No newline at end of file