Search⌘ K
AI Features

Methods of Structs

Explore how to declare and call methods inside Rust structs. Understand the role of &self, the impl block, and how methods help encapsulate data and behavior related to struct instances for better code organization.

What Are Methods?

Methods are just like user-defined functions. They are like functions, but the only difference lies in the fact that methods are declared specifically within the struct context.

Declare a Method

The method is like a regular function except that the &self parameter is passed to it and the items within the function are accessed through it.

self.item

Here self is the calling instance, i.e., it is referencing to ...