From 983cbbee4135f16c51225d3e7cc473a65765973c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Sun, 22 Dec 2024 05:08:26 +0300 Subject: [PATCH] feat: :sparkles: traits --- 26-generics/src/main.rs | 8 ++--- 27-traits/Cargo.toml | 6 ++++ 27-traits/src/main.rs | 72 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 27-traits/Cargo.toml create mode 100644 27-traits/src/main.rs diff --git a/26-generics/src/main.rs b/26-generics/src/main.rs index 65c0d5b..0341a02 100644 --- a/26-generics/src/main.rs +++ b/26-generics/src/main.rs @@ -22,16 +22,16 @@ impl Point { // This is a struct that can take any type into it. // Unlike "Point" its field not have to be same type but it can also be. -struct AnotherPoint { +struct AnotherPoint { x_axis: T, y_axis: U, - z_axis: K, + z_axis: V, } -impl AnotherPoint { +impl AnotherPoint { // This creates new point based from values that is given. // Their types not have to be same, they can be anything - fn new(x_axis: T, y_axis: U, z_axis: K) -> Self { + fn new(x_axis: T, y_axis: U, z_axis: V) -> Self { AnotherPoint { x_axis, y_axis, diff --git a/27-traits/Cargo.toml b/27-traits/Cargo.toml new file mode 100644 index 0000000..a9f9604 --- /dev/null +++ b/27-traits/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "traits" +version = "0.1.0" +edition = "2021" + +[dependencies] diff --git a/27-traits/src/main.rs b/27-traits/src/main.rs new file mode 100644 index 0000000..d4cebeb --- /dev/null +++ b/27-traits/src/main.rs @@ -0,0 +1,72 @@ +use std::fmt::{Debug, Display}; + +// This is a trait for defining shared behaviour. +// This trait only defines a rule about "T" which is a single type of generic. +// Here we defined a default behaviour by giving function body. +// If we didn't give this default, it should be implemented for every data type that shares this trait. +// I said Self needs to implement Debug trait because my default function body is printing in debug mode. +// I said "T" has to implement Display because I wanted to override default later for "Point" struct. +trait Tahinli { + fn print_data(&self) + where + Self: Debug, + T: Display, + { + println!("{:#?}", self) + } +} + +// This is a struct that must validate a type for every field in itself. +#[derive(Debug)] +struct Point { + x_axis: T, + y_axis: T, + z_axis: T, +} + +// We implemented our trait here and overrode default behaviour. +// Because I said "T" has to implement Display in both trait definition and here. +// I'm able to print every field of "Point" type by just println!. +impl Tahinli for Point { + fn print_data(&self) + where + T: Display, + { + println!("{}", self.x_axis); + println!("{}", self.y_axis); + println!("{}", self.z_axis); + } +} + +// This is my another struct that can accept at most 3 type of data types for different fields. +#[derive(Debug)] +struct AnotherPoint { + _x_axis: T, + _y_axis: U, + _z_axis: V, +} + +// I didn't override default implementation so this will follow default one. +impl Tahinli for AnotherPoint {} + +fn main() { + println!("Hello, world!"); + + // I'm creating a "Point" with only one type of data type for all fields which is i32 + let point = Point { + x_axis: 0, + y_axis: 1, + z_axis: 2, + }; + + // I'm creating "AnotherPoint" with 3 different data type for each different field. + let another_point = AnotherPoint { + _x_axis: '0', + _y_axis: "Tahinli", + _z_axis: 0.0, + }; + + // I'm able to print them but with different style because of overriding default function body. + point.print_data(); + another_point.print_data(); +}