Rust, Software Development

Custom Data Types With Struct, Trait, And Enum In Rust

Rust lang

We can create custom data types in Rust using struct, trait, and enum. A struct is a composite data type that groups variables in a memory block. These variables are then accessible via the struct’s instance – also referred to as an object in OOP languages. Moreover, a struct can have its methods (or functions) with implementations; and a trait can add new methods to it.

We can also use the enum keyword to create custom data types whose values are generally only those predefined in its definition.

Custom Type With Struct

There are three ways to define a struct, but only 2 are generally useful – a struct with named fields and a struct with unnamed fields. When we declare a variable of a struct type and initialize it, we are creating an instance of that struct type.

Consider the codes below – we created instances of a struct type in lines 9 and 16.

Struct With Named Fields

A custom data type based on struct has a name and may have named fields, which are variable declarations with variable names and only their data types.

In the following codes, we have a Person struct data type with two named fields – last_name and first_name, and we access them using the dot notation like in lines 13 and 14.

Struct With Unnamed Fields

A struct can have a definition without specifying named fields, and it works like a Tuple. In this case, we only define the data types. To access each field, we need to use its ordinal value with the struct instance. Consider the codes below – the struct type Coordinate has three unnamed fields. The first will have an ordinal value of 0, and the second will have an ordinal value of 1, and so on. Hence, coordinate_instance.0 and coordinate_instance.1 refer to the first and second fields, respectively.

Custom Type With Trait

A struct may have methods, and a trait can override or add new instance methods to it.

Struct With Methods

There are two types of methods a struct can have – static and instance methods. A static method does not require an instance of the struct to call it, while an instance method needs an instance of a struct to invoke it.

Struct With Methods And Trait

A trait can have zero or more instance methods without implementations for structs to implement.

Similar to structs, instance methods specify &self as their first function parameter.

To implement a trait for a struct, we use the impl keyword and provide implementations for trait methods.

What if we implement a trait with only a static method? The method becomes part of the struct as if we defined it in the struct. Consider the code snippet below.

With traits, we can use a custom data type based on a struct in different ways in our codes. For example, we can pass an instance to any functions that accept a parameter of any of the traits the struct implements.

Custom Type With Enum

We can also create custom data types with enum.

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *