Rust 101: Structures

Mukundh Bhushan
4 min readJun 26, 2020

--

In the previous article, we discussed about Modules in rust. Now let’s dive into Structures.

TL;DR

  • What are Structures?
  • Basic Structure
  • Assigning values to structure
  • Accessing values in a structure
  • Attributes used in structs
  • Fields with String datatypes
  • Structure with tuple
  • Using a structure in the field of another structure

First, let’s start off by creating a new project called “Structure” using the following command.

cargo new Structure --bin

What are Structures?

Rust is not an object-oriented programming language therefore it has no concept of objects and classes. Structures in Rust act as objects and when coupled with traits can together work as a class with methods defined under it. We will learn more about traits in the upcoming article.

Basic Structures

Structures in rust similar to other programming languages is a collection of similar or different datatypes to create a user defines datatype.

Here is the syntax to define a structure

pub struct <struct name>{   <field name>:<datatype>,
<field name>:<datatype>
}

By default, structures are private use “pub” to make them public

Defining a structure

Assigning values to structure

To assign values to the structure we need create a new instance of the structure, similar to instantiating an object in OOPs based programming languages.

As the variable “c” is not defined as mutable the values cannot be changed

Accessing values in a structure

The “.” operator is used to access the values in a structure.

Here is the syntax to access values in a struct

<struct var>.<filed name>

To change values assigned in a struct we need to declare the struct variable as a mutable type.

Here is the code for changing the value of the color blue

Structure with tuple

Structures that are defined with tuple do not have field names.
Here is the syntax

struct <struct name>(<datatype1>,<datatype2>,<datatype3>)

To access these elements, we use the “.” operator but instead of using the field name like the previous case we use the index.

Attributes used in structs

Attributes are tags which you can use to add metadata which can help change the execution of your code. Every function, structure can have thier own attributes.

There are 2 main attributes which are commonly used for structures

  • #[allow(dead_code)]
  • #[derive(Debug)]

#[allow(dead_code)]
Used when a structure declared but not used.

Consider the following code. Here the “color” struct is declared but not called. If this code is executed the compiler throws a warning “#[warn(dead_code)]”

To avoid this warning, we can add #[allow(dead_code)] to both of our structs

#[derive(Debug)]
Allows us to print the entire struct instead of only the fields

If you have tried to print the entire struct using “println!({:?},<struct var>)” it will throw an error. To print the entire struct instead of the field alone, we can use this attribute.

Fields with String datatypes

Passing a string as a datatype normally to a structure like an integer as shown above will throw an error

The above code is wrong. This is because the string reference is lost i.e. the. string is out of scope while declaring or using the struct.

There are two ways to fix it:

Defining a lifetime scope for the struct
We use ‘a is used to define lifetime scope

Using String::from()
The second method is to define the string using the inbuilt String class instead of a string pointer

Here is how the above code will look

Using a structure in the field of another structure

Consider the following case where there are two structures “Person” which contains the personal information ans Employee which contains the employees information. Employee uses the Person structure to fill up the personal details of the employees.

Note: the “#[derive(Debug)]” must be used in both the structures to print the Employee struct entirely.

In the next article, we will discuss about Traits and implementations.

link to the next article

Link to the previous article

--

--