Post

Method Syntax

Methods Syntax

They are defined within the context of a struct/enum/trait object, with the fn keyword, can have arguments and return values.

Their first parameter is self and represents an instance of the struct the method is being called on.

The example below defines a struct object called Car.

To define methods with the context of Car, we’ll add them within an impl(implementation) block.

We have 4 methods to define

  1. Create a new car instance with provided details
  2. Print a summary of the car’s information
  3. Check if the car is a specific color
  4. Increase the car’s model year by a specified amount (for simulation purposes)
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
struct Car {
  make: String,
  model: String,
  year: u16,
  color: String,
  num_doors: u8,
}

impl Car {
  // 1. Create a new car instance with provided details
  fn new(make: String, model: String, year: u16, color: String, num_doors: u8) -> Car {
    Car {
      make,
      model,
      year,
      color,
      num_doors,
    }
  }

  // 2. Print a summary of the car's information
  fn print_info(&self) {
    println!("Make: {}", self.make);
    println!("Model: {}", self.model);
    println!("Year: {}", self.year);
    println!("Color: {}", self.color);
    println!("Number of Doors: {}", self.num_doors);
  }

  // 3. Check if the car is a specific color
  fn is_color(&self, target_color: &str) -> bool {
    self.color.to_lowercase() == target_color.to_lowercase()
  }

  // 4. Increase the car's model year by a specified amount (for simulation purposes)
  fn age(&mut self, years: u16) {
    self.year += years;
  }
}

fn main(){
     let car = Car::new(String::from("Toyota"), String::from("CHR-25"), 2019, String::from("red"), 4);

     let color = "green";
     car.print_info();

     let is_color = car.is_color(&color);

     println!("color is {}",is_color);



}
This post is licensed under CC BY 4.0 by the author.