over the past couple of weeks I have been learning Rust, I figured I would do a post about using ‘impl’. The ‘impl’ keyword allows you to add functionality to your structs or enums, the way I remember this is alike to methods in classes when using C# or PHP.
using the below code as example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
enum Color { Red, Yellow, } impl Color { fn print(&self) { match self { Color::Red => println!("red"), Color::Yellow => println!("yellow"), } } } |
I have created an enum called ‘Color’, using the ‘impl’ keyword I have created a function called ‘print’ this function is now part of the Color enum. it has allowed the Color enum to have “functionality” in this case to print some details out.
likewise in this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
struct Dimensions { width: f64, height: f64, depth: f64, } impl Dimensions { fn print(&self) { println!("Width: {:?}", self.width); println!("Height: {:?}", self.height); println!("Depth: {:?}", self.depth); } } |
I have added functionality to the ‘Dimensions’ struct, a function called ‘print’ which will print out the details of the dimensions.
in the below code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
struct ShippingBox { color: Color, weight: f64, dimensions: Dimensions } impl ShippingBox { fn new(weight: f64, color: Color, dimensions: Dimensions) -> Self { Self { weight, color, dimensions, } } fn print(&self) { self.color.print(); self.dimensions.print(); println!("weight: {:?}", self.weight); } } |
I have added functionality to the ‘ShippingBox’ struct, two functions ‘new’ and ‘print’. the ‘new’ function is responsible for creating a new ShippingBox and uses ‘Self’ with a capital ‘S’ to do this.
finally the code below, shows how all of this is tied together.
1 2 3 4 5 6 7 8 9 10 |
fn main() { let small_dimensions = Dimensions { width: 1.0, height: 2.0, depth: 4.0, }; let small_box = ShippingBox::new(5.0, Color::Red, small_dimensions); small_box.print(); } |
full code is shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
enum Color { Red, Yellow, } impl Color { fn print(&self) { match self { Color::Red => println!("red"), Color::Yellow => println!("yellow"), } } } struct Dimensions { width: f64, height: f64, depth: f64, } impl Dimensions { fn print(&self) { println!("Width: {:?}", self.width); println!("Height: {:?}", self.height); println!("Depth: {:?}", self.depth); } } struct ShippingBox { color: Color, weight: f64, dimensions: Dimensions } impl ShippingBox { fn new(weight: f64, color: Color, dimensions: Dimensions) -> Self { Self { weight, color, dimensions, } } fn print(&self) { self.color.print(); self.dimensions.print(); println!("weight: {:?}", self.weight); } } fn main() { let small_dimensions = Dimensions { width: 1.0, height: 2.0, depth: 4.0, }; let small_box = ShippingBox::new(5.0, Color::Red, small_dimensions); small_box.print(); } |