Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add example about wrapper pattern, Also fix missing example for associated types in trait #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 8 additions & 42 deletions traits/traits_2/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,48 +1,14 @@
mod calculation_spec;
mod two_dimensional;
use std::fmt::{write, Display};

use crate::calculation_spec::Area;
struct Wrapper(Vec<String>);

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<T: Area>(name: String, item: &T) {
println!("{} area: {}", name, item.calculate());
}

fn calculate_and_print_result3<T>(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<T>(name: String, item: &T)
where
T: Area,
{
println!("{} area: {}", name, item.calculate());
}
8 changes: 8 additions & 0 deletions traits/traits_3/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
60 changes: 60 additions & 0 deletions traits/traits_3/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<T: Area>(name: String, item: &T) {
println!("{} area: {}", name, item.calculate());
}

fn calculate_and_print_result3<T>(name: String, item: &T)
where
T: Area,
{
println!("{} area: {}", name, item.calculate());
}

fn calculate_and_print_result4<T>(name: String, item: &T)
where
T: Area,
{
println!("{} area: {}", name, item.calculate());
}
8 changes: 8 additions & 0 deletions traits/traits_4/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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]
11 changes: 11 additions & 0 deletions traits/traits_4/src/circle.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
14 changes: 14 additions & 0 deletions traits/traits_4/src/main.rs
Original file line number Diff line number Diff line change
@@ -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());
}

5 changes: 5 additions & 0 deletions traits/traits_4/src/shape.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub trait Shape {
type Area;

fn area(&self) -> Self::Area;
}
11 changes: 11 additions & 0 deletions traits/traits_4/src/square.rs
Original file line number Diff line number Diff line change
@@ -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
}
}