Structures in Rust

A Structure is a user-defined data type which represents not only a single data type but can represent multiple data types using the same structure name.

As we have seen in Rust arrays, where only multiple values can be stored but of a single data type. The structure in Rust is an advanced version of arrays because here we can store not only multiple values of the same type but multiple values of different data types.

Graphical representation of a Rust Structure:

rust-structure01

In the above diagram, we have seen a basic model of a structure. It is holding values of integer type, char type, etc.

Implementation of structure in Rust:

A structure can be used in programming the same way, an array is used. Just the way to access or retrieve the data of a structure is little different from an array.

First of all, a structure should be declared before we can use it, once a structure is declared, we can store values in it.

Syntax for Structure Declaration :

struct Structure_name {
      // field_1: datatype
      // field_2: datatype
      // field_3: datatype
}

Syntax for Structure initialization :

 let value = Structure__name {
      field_1: data1,
      field_2: data2
};

In the above initialization, we have seen, that the structure name should start with a capital letter- Structure_name . Then the field_1 and field_2 are assigned values data1 and data2 respectively.

Now the structure can be accessed the value and a dot (.) operator.

value.field_1

Suppose, we want to print the field_1, we shall use the macro println!() and use the delimiter {}, in this way:

println!("{}", value.field_1)

Program to print details of a student using a structure :

 struct Student {
   name: String,
   school: String,
   roll: u32,
   grade: char

 }
 fn main() {
   let value = Student {
     name: String::from("Fitas"),
     school: String::from("Chercher.tech"),
     roll: 01,
     grade: 'A'

   };
   println!("Student: {} of {} school bearing roll {} has obtained {} grade", value.name, value.school, value.roll, value.grade);
 }

In the above program, we have declared a structure Student, inside which we have four different fields- name, school, roll, and grade, with data types String, String, u32(unsigned 32-bit integer) and character respectively.

Inside the main() function, the structure Student has been initialized.

Note: The closing brace } of the structure must follow a semi-colon (;).

Program Output :

rust-structure-demo-output

Structure as Function Parameter

We can pass the structure to a function as a parameter. We shall see a program below, how an instance of a structure can be passed as a parameter and how the field values of the structured can be retrieved using the function.

 struct Student {
     name: String,
     school: String,
     roll: u32,
     grade: char
 }
 fn main() {
     let value1 = Student {
         name: String::from("Fitas"),
         school: String::from("Chercher.tech"),
         roll: 01,
         grade: 'A'
     };
     display(value1);
     fn display(value: Student) {
         println!("Student: {} of {} school bearing roll {} has obtained {} grade", value.name, value.school, value.roll, value.grade);
     }
 }

In the above program, we have seen that the structure has been declared before the main() function. Then we have created one instance of the structure- value1 in which different field data has been initialized.

Then display() function is created inside which we have defined a macro println!(), which prints the attribute value of the structure.

Now the instance of the structure can be passed directly to the function display(), to print the structure data.

Program Output :

rust-structure-instance-as-function-parameter-one

Initializing structure out of its scope

So far, we have seen that the structure can be initialized only inside the scope of it, that is inside the two braces { and }. But the structure field can also be initialized outside the braces. It can be done using a dot operator to access the field inside the structure.

Structure_instance_name.field_name= data;

Program :

 struct Student {
     name: String,
     school: String,
     roll: u32,
     grade: char
 }
 fn main() {
     let mut value = Student {
         name: String::from("Fitas"),
         school: String::from("Chercher.tech"),
         roll: 01,
         grade: 'A'
     };
     value.grade = 'C';
     println!("Student: {} of {} school bearing roll {} has obtained {} grade", value.name, value.school, value.roll, value.grade);
 }

In the above program, the value is an instance of the structure Student. And to overwrite the field grade we have muted the instance, and then reassigned the variable value with a character C outside the structure initialization.

Program Output :

rust-structure-modify

In the above program output, we have seen that the grade which was earlier A has been now changed into C.

Please note that, if the field value has to be modified or initialized outside the scope of the instance must be muted, otherwise the field value cannot be overwritten and the compiler will generate an error.

Returning structure using a function

A structure instance can also be returned using a function. In our article Rust Functions, we have seen how the value of a particular data type is returned using the functions.

A structure instance can be returned by a function, in the following way:

fn function_name(instance_name: Structure_name) -> Structure_Name {
 return instance_name;
}

A program to compare the roll of two students and list in ascending order.

 struct Student {
     name: String,
     school: String,
     roll: u32,
     grade: char
 }
 fn main() {
     fn student_detail() - > Student {
         let value1 = Student {
             name: String::from("Fitas"),
             school: String::from("Chercher.tech"),
             roll: 01,
             grade: 'A'
         };
         return value1;
     }
     let order = student_detail();
     println!("Student: {} of {} school bearing roll {} has obtained {} grade", order.name, order.school, order.roll, order.grade);
     println!("the details in which roll is less is printed above");
 }

As shown in the program, we have defined a function called student_detail() and mentioned the return type as Student(which in our case is the structure). So, it means, that the return type is a Student-type structure.

Program Output :

rust-structure-return

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions