Working with Rust: Understanding struct, impl, methods and self

While working with any programming language, we always have a requirement of creating custom data types. If you are familiar with any object-oriented language, there are classes that comprise data attributes and some methods to play with their context. The objects of these classes are used for reflecting the behaviors.

Like other languages, Rust also provides us with all these flexibilities, but in its own way. Here in Rust, we have the following terms:

struct: A struct (also called structure), is a custom data type that lets you name and package multiple related values. It starts with the keyword “struct”.

impl: An impl is an implementation block that has the keyword “impl” followed by the same name as that of the struct. It is used to comprise methods, and functions. A struct is allowed to have multiple impl blocks.

Methods: Methods are similar to functions, they’re declared with the “fn” keyword followed by their name, they can have parameters and a return value. However, methods are different from functions because they’re defined within the context of a struct and their first parameter is always “self”.

self: “self” keyword represents the instance of the struct, the method is being called on. “self” when used as the first method argument, is a shorthand for “self: Self”. There are also “&self”, which is equivalent to “self: &Self”, and “&mut self”, which is equivalent to “self: &mut Self”. Self in method arguments is syntactic sugar for the receiving type of the method (i.e. the type whose impl this method is in). This also allows for generic types without too much repetition.

Associated Functions: Functions inside an impl block that do not take “self” as a parameter are called associated functions because they’re associated with the struct. They’re still functions, not methods, because they don’t have an instance of the struct to work with. Associated functions are often used for constructors that will return a new instance of the struct.

Now let’s have a code implementation to look how this combination of struct, impl, methods, and self, work together.

Now let’s have an understanding of what’s going on.

There’s a struct named Employee having two value types named emp_id and emp_name respectively.

There is an implementation block having the same name as that of the struct, i.e. Employee.

The implementation block has two methods and one associated function.

The method “show_details” is basically borrowing an immutable reference, and showing the details stored in the reference. For this reference to be borrowed, & is used with “Self”. We could also write just “&self” instead of “self: &Self”. That would be using the syntactic sugar.

The next method is “mutate_and_show_details”, this method is basically taking a mutable reference so that the values of the employee instance could be modified. Here we are borrowing a mutable reference.

And then the last method “create_employee”, is the associated function. This function is basically used to return an instance of Employee structure.

In the main method, we have created an immutable instance and sent its reference to show_details.

Then we used the shadowing feature of Rust and created a mutable instance and sent its reference to mutable_show_details.

We have called show_details again to showcase that the value for employee instance is actually changed because of mutability.

And in the end, we are calling the associated function to get a new instance of Employee.

So this was a brief overview of how struct and impl are used along with methods and associated functions to provide the custom data types along with a behavior to work with.

This blog might be creating some curiosity regarding the mutable and immutable references and shadowing being used in the main method. If yes, then keep going through our blogs, because we will soon be covering the hot topics like mutability and shadowing in Rust. For more information on Rust, keep going through our blogs, as we at Knoldus believe in working on the cutting-edge technologies and help out the community of tech-enthusiasts by sharing our experiences and knowledge.

Like this:

Anmol Mehta is a Software Consultant having experience of more than 1.5 years. A keen programmer who has experience in Scala and Java. He is recognized as a dedicated and determined team player who enjoys working on new technologies. He is a professional and a technology enthusiast. He believes in the approach that “teamwork makes the dream work”. He is a quick and always-on learner who ensures better quality in every increment of his work.
He believes in abiding standard coding practices. He always looks after that the team is working in sync with each other both at the technical and managerial level.