Methods of Structs

This lesson will get you acquainted with the methods in structs, i.e., what are they and how to create them in structs?

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 the struct.

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy