OneBite.Dev - Coding blog in a bite size

Struct Method in Rust

How to add a method on a struct in Rust. If we do have a special repeatable task for a struct, it's better to put it, in it's own function

How to add a method on a struct in Rust. If we do have a special repeatable task for a struct, it’s better to put it, in it’s own function.

You can say, method is same as function, but it’s defined in the struct context (or enum/ or object).

Here is a rectangle Struct, with area method, to count it’s area

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

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

How to call the method

Just like a function, but with the instance name of the struct

fn main() {
    let rect1 = Rectangle {
        width: 30,
        height: 50,
    };

    println!(
        "The area of the rectangle is {} square pixels.",
        rect1.area()
    );
}

Method is useful to organize our code, as method only related to that particular struct.

Associated functions

Associated functions are normally used for constructors that return a new instance of the struct.

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn square(size: u32) -> Rectangle {
        Rectangle {
            width: size,
            height: size,
        }
    }
}

How to call ‘square’ the function?

fn main() {
    let sq = Rectangle::square(3);
}

On calling an associated function, we use the Struct with double colon (::) and the function’s name

← Struct data type in Rust...
Enums in Rust... →
rust