From 433bb4f1efe89b6fab703e193602cea2beda9f24 Mon Sep 17 00:00:00 2001 From: afifurrohman-id Date: Sun, 5 May 2024 18:16:42 +0700 Subject: [PATCH] feat: Add example about wrapper pattern, Also fix missing example for associated types in trait --- traits/traits_2/src/main.rs | 50 +++------------- traits/traits_3/Cargo.toml | 8 +++ .../src/calculation_spec.rs | 0 traits/traits_3/src/main.rs | 60 +++++++++++++++++++ .../src/two_dimensional.rs | 0 traits/traits_4/Cargo.toml | 8 +++ traits/traits_4/src/circle.rs | 11 ++++ traits/traits_4/src/main.rs | 14 +++++ traits/traits_4/src/shape.rs | 5 ++ traits/traits_4/src/square.rs | 11 ++++ 10 files changed, 125 insertions(+), 42 deletions(-) create mode 100644 traits/traits_3/Cargo.toml rename traits/{traits_2 => traits_3}/src/calculation_spec.rs (100%) create mode 100644 traits/traits_3/src/main.rs rename traits/{traits_2 => traits_3}/src/two_dimensional.rs (100%) create mode 100644 traits/traits_4/Cargo.toml create mode 100644 traits/traits_4/src/circle.rs create mode 100644 traits/traits_4/src/main.rs create mode 100644 traits/traits_4/src/shape.rs create mode 100644 traits/traits_4/src/square.rs diff --git a/traits/traits_2/src/main.rs b/traits/traits_2/src/main.rs index c14f6f3..9c768ce 100644 --- a/traits/traits_2/src/main.rs +++ b/traits/traits_2/src/main.rs @@ -1,48 +1,14 @@ -mod calculation_spec; -mod two_dimensional; +use std::fmt::{write, Display}; -use crate::calculation_spec::Area; +struct Wrapper(Vec); -fn main() { - // let circle_one = two_dimensional::Circle{ radius: 10 }; - let circle_one = new_circle(5); - // println!("circle area: {}", circle_one.calculate()); - calculate_and_print_result("circle".to_string(), &circle_one); - - // let square_one = two_dimensional::Square{ length: 5 }; - let square_one = new_square(10); - // println!("square area: {}", square_one.calculate()); - calculate_and_print_result("square".to_string(), &square_one); -} - -fn new_circle(radius: i32) -> impl Area { - let data = two_dimensional::Circle{ - radius - }; - data -} - -fn new_square(length: i32) -> impl Area { - two_dimensional::Square{ - length +impl Display for Wrapper { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + write(f, format_args!("{:?}", self.0)) } } -fn calculate_and_print_result(name: String, item: &impl Area) { - println!("{} area: {}", name, item.calculate()); -} - -fn calculate_and_print_result2(name: String, item: &T) { - println!("{} area: {}", name, item.calculate()); -} - -fn calculate_and_print_result3(name: String, item: &T) where T: Area { - println!("{} area: {}", name, item.calculate()); +fn main() { + let v = Wrapper(vec![String::from("a"), String::from("b")]); + println!("{}", v) } - -fn calculate_and_print_result4(name: String, item: &T) -where - T: Area, -{ - println!("{} area: {}", name, item.calculate()); -} \ No newline at end of file diff --git a/traits/traits_3/Cargo.toml b/traits/traits_3/Cargo.toml new file mode 100644 index 0000000..f034e41 --- /dev/null +++ b/traits/traits_3/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "traits_3" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/traits/traits_2/src/calculation_spec.rs b/traits/traits_3/src/calculation_spec.rs similarity index 100% rename from traits/traits_2/src/calculation_spec.rs rename to traits/traits_3/src/calculation_spec.rs diff --git a/traits/traits_3/src/main.rs b/traits/traits_3/src/main.rs new file mode 100644 index 0000000..3d5c56c --- /dev/null +++ b/traits/traits_3/src/main.rs @@ -0,0 +1,60 @@ +mod calculation_spec; +mod two_dimensional; + +use crate::calculation_spec::Area; + +trait Message { + fn log(&self); +} + +impl Message for String { + fn log(&self) { + println!("{}", self); + } +} + +fn main() { + let s = String::from("hello"); + s.log(); + + // let circle_one = two_dimensional::Circle{ radius: 10 }; + let circle_one = new_circle(5); + // println!("circle area: {}", circle_one.calculate()); + calculate_and_print_result("circle".to_string(), &circle_one); + + // let square_one = two_dimensional::Square{ length: 5 }; + let square_one = new_square(10); + // println!("square area: {}", square_one.calculate()); + calculate_and_print_result("square".to_string(), &square_one); +} + +fn new_circle(radius: i32) -> impl Area { + let data = two_dimensional::Circle { radius }; + data +} + +fn new_square(length: i32) -> impl Area { + two_dimensional::Square { length } +} + +fn calculate_and_print_result(name: String, item: &impl Area) { + println!("{} area: {}", name, item.calculate()); +} + +fn calculate_and_print_result2(name: String, item: &T) { + println!("{} area: {}", name, item.calculate()); +} + +fn calculate_and_print_result3(name: String, item: &T) +where + T: Area, +{ + println!("{} area: {}", name, item.calculate()); +} + +fn calculate_and_print_result4(name: String, item: &T) +where + T: Area, +{ + println!("{} area: {}", name, item.calculate()); +} diff --git a/traits/traits_2/src/two_dimensional.rs b/traits/traits_3/src/two_dimensional.rs similarity index 100% rename from traits/traits_2/src/two_dimensional.rs rename to traits/traits_3/src/two_dimensional.rs diff --git a/traits/traits_4/Cargo.toml b/traits/traits_4/Cargo.toml new file mode 100644 index 0000000..778706b --- /dev/null +++ b/traits/traits_4/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "traits_4" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/traits/traits_4/src/circle.rs b/traits/traits_4/src/circle.rs new file mode 100644 index 0000000..20221e6 --- /dev/null +++ b/traits/traits_4/src/circle.rs @@ -0,0 +1,11 @@ +pub struct Circle { + pub radius: f64, +} + +impl crate::shape::Shape for Circle { + type Area = f64; + + fn area(&self) -> Self::Area { + std::f64::consts::PI * self.radius * self.radius + } +} diff --git a/traits/traits_4/src/main.rs b/traits/traits_4/src/main.rs new file mode 100644 index 0000000..51726d6 --- /dev/null +++ b/traits/traits_4/src/main.rs @@ -0,0 +1,14 @@ +mod circle; +mod shape; +mod square; + +use crate::shape::Shape; + +fn main() { + let obj1 = circle::Circle { radius: 10.0 }; + println!("area of circle: {:.2}", obj1.area()); + + let obj2 = square::Square { side: 10 }; + println!("area of square: {:}", obj2.area()); +} + diff --git a/traits/traits_4/src/shape.rs b/traits/traits_4/src/shape.rs new file mode 100644 index 0000000..715e5b5 --- /dev/null +++ b/traits/traits_4/src/shape.rs @@ -0,0 +1,5 @@ +pub trait Shape { + type Area; + + fn area(&self) -> Self::Area; +} diff --git a/traits/traits_4/src/square.rs b/traits/traits_4/src/square.rs new file mode 100644 index 0000000..82b11b7 --- /dev/null +++ b/traits/traits_4/src/square.rs @@ -0,0 +1,11 @@ +pub struct Square { + pub side: i64, +} + +impl crate::shape::Shape for Square { + type Area = i64; + + fn area(&self) -> Self::Area { + self.side * self.side + } +}