Structure your data in Rust by Amita Yadav

Rust uses some of the features of functional programming, object orientated programming and uses structures to structure the data.

In this article by Software Consultant Amita Yadav, you can find out exactly what Structure is as well as the functionality of class and objects in Rust.

'Structuring of data in the correct way is very important. In Java and C++, we use Class & Object. In C, we use structures, unions, and enums.

We know that Rust adopts some features of functional programming and OOPS. Class and object is the main weapon of OOPS‘ feature but how rust provides that functionality in its context. We will see this:

As Rust is a bit similar to C, so it also uses structures to structure the data.

What is Structure?

A struct, or structure, is a user-defined data type that lets you create a data type that can be used to group items of possibly different types into a single entity. There are three types of structures:

Classical struct

Tuple Struct

Unit Struct

How to create a classical struct?

To create and define a struct, you enter the keyword struct and name the entire struct. A struct’s name should describe the significance of the pieces of data being grouped together. Then, inside curly brackets, we define the names and types of the pieces of data, which we callfields. For example, a struct that stores information about a user account.

To use a struct after we’ve defined it, we create an instance of that struct by specifying concrete values for each of the fields. We don’t have to specify the fields in the same order in which we declared them in the struct.For example,

Functionality of Class and Objects in Rust

The Gang of Four book is a catalogue of object-oriented design patterns. It defines OOPs this way:

Object-oriented programs are made up of objects. An object packages both data and
the procedures that operate on that data. The procedures are typically called methods or operations.

Using this definition, Rust is object-oriented: structs and enums have data, and impl blocks provide methods on structs and enums. Even though structs and enums with methods aren’t called objects, they provide the same functionality, according to the Gang of Four’s definition of objects.

Functionality of Constructors in Rust

Note that the entire instance must be mutable; Rust doesn’t allow us to mark only certain fields as mutable. As with any expression, we can construct a new instance of the struct as the last expression in the function body to implicitly return that new instance. For example,

Type of struct

1. Tuple Structs

When you want to give the whole tuple a name and make the tuple be a different type than other tuples, you can use tuple structs without named fields to create different types. For example, here are definitions and usages of two tuple structs named Color and Point:

2. Unit Structs

You can also define structs that don’t have any fields! Unit-like structs can be useful in situations in which you need to implement a trait on some type but don’t have any data that you want to store in the type itself. For example,

struct Nil;

I hope this blog covers all your doubts about how to structure your data in rust as compared to other programming languages.

Archive

Signify Technology is an innovative technology recruitment business with offices based in London & Los Angeles. We provide permanent and contract technology recruitment solutions to a wide range of the world's leading brands on a global basis including blue chip, technology companies and start-ups. Behind each piece of technology changing the world, is a person, it's our mission to introduce them to the companies that need them the most.