How to Implement Methods on a Struct in Rust

Define an impl block for your struct and add functions with self as the first parameter to create methods.

Implement methods on a struct by defining an impl block for the struct and adding functions that take &self or &mut self as the first parameter.

struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

The &self parameter allows the method to read the struct's data without taking ownership, while &mut self would allow modification.