1 of 10

Implementation

2 of 10

Implementation

Inherent Implementation

Implementation

Trait Implementation

Generic Implementation

Trait bound impl

Blanket impl

đŸĻ€impl block for a struct automatically gets access to the fields of that struct.that’s why we used Instead of fn area(w:&width, h:&hight) || fn area(&self)

đŸĻ€Rust impl mean define method / function for (struct/enum/trait)

3 of 10

Here we define the method for the struct/enum itself.

Here Point::new is like a constructor and distance_from_origin is an instance method.

struct Circle {

radius: f64,

}

impl Circle {

fn area(&self) -> f64 {

3.1416 * self.radius * self.radius

}

fn new(r: f64) -> Self {

Self { radius: r }

}

}

Use:

let c = Circle::new(5.0);

println!("Area = {}", c.area());

We directly write method for the struct and enum inherent

implementation.

Inherent Implementation

4 of 10

Trait Implementation

  • Trait is behavior contract
  • impl is implementation of the contractāĨ¤

5 of 10

Generic Implementation

struct Point<T> {

x: T,

y: T,

}

impl<T> Point<T> {

fn new(x: T, y: T) -> Self {

Self { x, y }

}

}

Used

let p1 = Point::new(10, 20);

let p2 = Point::new(1.1, 2.2);

impl<T> āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āĻšā§Ÿ āϝāĻ–āύ struct/enum generic type āĻ¨ā§‡ā§ŸāĨ¤īŋŊ

impl<TypeParam> → āϏāĻŦ āϟāĻžāχāĻĒ⧇āϰ āϜāĻ¨ā§āϝ method define āĻšāĻŦ⧇

  • impl<T> use when struct/enum generic type is unknownāĨ¤īŋŊ
  • impl<TypeParam> → it can take any type in method define āĨ¤īŋŊ

6 of 10

Generic Implementation

Unknown type in method

With Generic it can take any Unknown type in method

7 of 10

Generic + Trait Bound impl

  • impl<T: Display> → āĻāĻ–āĻžāύ⧇ T āύāĻžāĻŽā§‡ generic āϟāĻžāχāĻĒ āφāϛ⧇āĨ¤īŋŊ
  • T: Display āĻŽāĻžāύ⧇ āĻšāϞ⧋: T āϟāĻžāχāĻĒ āĻ…āĻŦāĻļā§āϝāχ Display trait implement āĻ•āϰāϤ⧇ āĻšāĻŦ⧇āĨ¤īŋŊ
  • āĻ…āĻ°ā§āĻĨāĻžā§Ž, Rust āĻ•āĻŽā§āĻĒāĻžāχāϞāĻžāϰ compile-time āĻ enforce āĻ•āϰ⧇ āϝ⧇ T āϝāĻĻāĻŋ Display āύāĻž āĻ•āϰ⧇, āϤāĻžāĻšāϞ⧇ show() method āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āϝāĻžāĻŦ⧇ āύāĻžāĨ¤
  • Purpose ]
  • Conditional implementationāĨ¤īŋŊCode āϕ⧇ āĻŦ⧇āĻļāĻŋ type-safe āφāϰ expressive āĻ•āϰ⧇āĨ¤

use std::fmt::Display;

struct Wrapper<T>(T);

// Trait bound: T must implement Display

impl<T: Display> Wrapper<T> {

fn show(&self) {

println!("Value = {}", self.0);

}

}

  • If you give Trait bound (T: Trait) then the method only available for that trait implement.

Used:

let w = Wrapper(42);

w.show();

// works, āĻ•āĻžāϰāĻŖ i32 implements Display

8 of 10

Trait Bound impl

Trait bound means you are giving a condition →T: Display

“This method/function will only work on types that implement Display trait.”

9 of 10

Blanket impl

[ Rule ]

  • impl<T: Trait> āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāϞ⧇ āĻ…āύ⧇āĻ•āϗ⧁āϞ⧋ āϟāĻžāχāĻĒ⧇āϰ āϜāĻ¨ā§āϝ āĻāĻ•āϏāĻžāĻĨ⧇ implementation āĻĻ⧇āĻ“ā§ŸāĻž āϝāĻžā§ŸāĨ¤
  • āĻāϟāĻžāϕ⧇ blanket implementation āĻŦāϞāĻž āĻšā§ŸāĨ¤

[ Purpose ]

  • Standard library-āϤ⧇ āĻĒā§āϰāϚ⧁āϰ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻšā§Ÿ (āϝ⧇āĻŽāύ impl<T: ?Sized> AsRef<T> for T { ... })āĨ¤

trait Greet {

fn greet(&self);

}

impl<T: ToString> Greet for T {

fn greet(&self) {

println!("Hello, {}", self.to_string());

}

}

Using impl<T: Trait> allows you use multiple types at once.

Used:

"Rust".greet();

123.greet();

10 of 10

Blanket impl

Using impl<T: Trait> allows you use multiple types at once.

Blanket Impl = Implementing a trait for all applicable types at once.