feat: traits

This commit is contained in:
Ahmet Kaan GÜMÜŞ 2024-12-22 05:08:26 +03:00
parent e6a2ecd003
commit 983cbbee41
3 changed files with 82 additions and 4 deletions

View file

@ -22,16 +22,16 @@ impl<T> Point<T> {
// This is a struct that can take any type into it. // 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. // Unlike "Point" its field not have to be same type but it can also be.
struct AnotherPoint<T, U, K> { struct AnotherPoint<T, U, V> {
x_axis: T, x_axis: T,
y_axis: U, y_axis: U,
z_axis: K, z_axis: V,
} }
impl<T, U, K> AnotherPoint<T, U, K> { impl<T, U, V> AnotherPoint<T, U, V> {
// This creates new point based from values that is given. // This creates new point based from values that is given.
// Their types not have to be same, they can be anything // 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 { AnotherPoint {
x_axis, x_axis,
y_axis, y_axis,

6
27-traits/Cargo.toml Normal file
View file

@ -0,0 +1,6 @@
[package]
name = "traits"
version = "0.1.0"
edition = "2021"
[dependencies]

72
27-traits/src/main.rs Normal file
View file

@ -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<T> {
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<T> {
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<T>" type by just println!.
impl<T> Tahinli<T> for Point<T> {
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<T, U, V> {
_x_axis: T,
_y_axis: U,
_z_axis: V,
}
// I didn't override default implementation so this will follow default one.
impl<T, U, V> Tahinli<T> for AnotherPoint<T, U, V> {}
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();
}