Array

Array in Rust is nothing but an object to store multiple values of the same data-type. Simply, an array is a homogeneous collection of values of the same data-type.

Pictorial Representation of an Array :

rust-array

The above diagram is a representation of an array. This array holds integer type values.

An array is different from a variable in this way:

  • A variable can store only one value at a time. But an array can store multiple values at a time.
  • A variable store the values in a random location, therefore making it difficult to retrieve the data when required. But, in the array, the data or values can be stored anywhere, but the retrieval can be done simply using the array index, which is much faster and feasible.
  • A variable may not be feasible when multiple values of the same data type are to be stored, as different variables have to be declared and initialized in that case. But, in the array, the array name can be used to initialize different values, just with the help of the array index.

Implementing Array in Rust:

Arrays can be implemented through Rust programming, in different ways.

  • Declaring the array with data types and initializing with data values.
  • Declaring and initializing the array without mentioning the data types.
  • Declaring the array just mentioning the size of the array and the data values.
  • Declaring the array and then initializing it with the help of a for Loop.

There can also be different ways of declaring and initializing an Array. Below we shall see one by one:

Simple Array

A simple array is nothing, but just an array. Here, we just mention the data type and the size of an array.

Syntax :

 let array_name:[data_type; size_of_array] = [data_1,data_2,data_3,...]

In the above syntax, the array is the array_name and inside the [ ], we have the data_type (the specific type of values ) the array is going to store, and the size of the array. After the assignment operator, we have the data values separated by ,.

Program to store five integer values inside an array :

fn main(){
   let my_array:[i32;5] = [10,20,30,40,50];
   println!("The values in the array is {:?}",my_array);
   
}

As shown in the above program, we have the array called my_array and its data-type is i32 and the size of the array is 5. The values assigned to the array is - 10, 20, 30, 40 and 50. Then using the macro println!(), we have directly tried to print the contents of the array.

It may be noted that the {:?} delimiter is used inside the macro to print the array contents.

Program Output :

rust-array-simple

Array declaration without data type and size

An array can also be declared and initialized directly without, mentioning the data type and the size. An array can be directly initialized with values and accordingly the data type and the length of the array is automatically set.

Syntax :

 let array_name = [value_1, value_2, ...];

This is to keep in mind that, since an array will keep values of single data type only, that is why all the values should be of the same data type.

Program to store five values in an array and print the values :

fn main(){
   let my_array = [10,20,30,40,50];
   println!("array is {:?}",my_array);
   
}

Program Output :

rust-array-direct-init

In the program output, we have seen that the program has printed output serially, we provided in the array.

Array declaration to print default values

In Rust Array, there can be arrays which are not initialized, and only declared, but still print values in default mode. This type of array is useful when we need similar values inside an array. The number of values depends on the length of the array, which should be declared by the user.

Syntax :

let array_name:[data_type;size_of_array] = [default_value;size_of_array];

Program to print a default value of an array :

fn main() {
   let my_array:[i32;4] = [9;4];
   println!("The default values of the array is: {:?}",my_array);
   
}

In the above program, we have just mentioned the data type as i32 and the size of the array and default value.

Program Output :

rust-array-default-value

In the above program output, it is seen that no values are initialized inside the array, as we did earlier. The default value is automatically copied into the array, which of length 4 and so the same value is printed 4 times.

This array has been initialized by the program itself and is exactly same as the arrays we have seen earlier. Therefore, the array may be printing the same value four times, but all the values are stored in separate indexes of the array, which means it is following the property of an array.

Array with a for loop:

An array index starts from 0 and ends with n-1 (where n is the maximum size of an array). Now, when we print an array directly, without mentioning any index value, then the complete contents of the array is printed in default.

Now, one may be interested to print only some specific values of an array, following some particular pattern for the indexes.

Let's Say,

  • I want data in those array cells whose indexes are odd numbers and starts from 1. That is - array[0], array [1], array[3], array[5], and so on.
  • I want data the first data of an array of any five subsequent order and starts from 0. That is- array[0], array[5], array[11], and so on.
  • I want data from the array cell whose index in a multiple of 3. That is- array[3], array[6], array[9], and so on.
  • The list of requirements will go on.

This type of customization can be done by using a for loop.

Syntax :

for loop_variable in lower_limit..upper_limit {
      println!("{}",array_name[loop_variable]);
   }

Program to print the contents of an array using a for loop :

fn main() {
 let my_array: [i32;4] = [10, 20, 30, 40];
 for index in 0. .4 {
  println!("The content of the array in index {} is : {}", index, my_array[index]);
 }
}

In the above program, we have declared an array called my_array and initialized it with data- 10, 20, 30, 40 and 50. Now, inside the for loop, we have the loop variable- index, which starts from 0 and increments up to 4 till the loop ends.

So, inside the scope of the for loop, we have used the macro println!() to print the contents of the array with respective index. Therefore, the my_array[0] is the first one to be printed. And then the loop variable index increments to 1. And so next, my_array[1] will be printed and so on.

Output :

rust-array-using-for-loop

Customizing the for loop:

We can also retrieve particular data from specific indexes of an array. This can be simply done by setting a conditional statement inside the for loop. If the condition is satisfied, the index value is retrieved, otherwise not. We shall use the decisional if statement to do that.

Program to print data from an array whose index is an even number:

fn main() {
 let my_array: [i32;10] = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100];
 for index in 0. .10 {
  if index % 2 == 0 {
   println!("The content of the array in the even index {} is : {}", index, my_array[index]);
  }
 }
}

In the above program, we have initialized the array my_array. Now we want only those values to be printed whose index is an even number. So, we have set a conditional if statement to check whether the loop variable - index is an even number. If yes, ( if index % 2 == 0) then the value corresponding to that index, gets printed.

Program Output :

rust-array-conditional-display

Fetching elements of an array using the iter() function

The elements of an array can be directly fetched using the function iter(). Syntax to use the iter() function is:

for variable_name in array_name.iter(){
println!("{}", variable_name);
}

Program to print att the elements in an array :

fn main() {

 let my_array: [i32;5] = [1, 2, 3, 4, 5];
 for element in my_array.iter() {
  println!("The elements in the array are :{}", element);
 }
}

In the above program, the array my_array is initialized with integer values. Then using the iter() function, the values are using the printlnl!() macro.

Program Output :

rust-array-iter-function

Initializing Array outside the declaration:

Rust does not support mutability of variables in default. It means, that once a variable is assigned with some values, it cannot be overwritten inside the program. To overwrite or change a variable, we must mute the variable and then can assign it to a different value.

Similar, in the case of an array. When an array of any size is assigned with some values. The cells of the arrays stores the values with appropriate indexes. Now, it is possible to refer an index of an array and change the corresponding value inside the cell.

But, this can only be done by muting the array, at the time of its declaration and initialization. Therefore, first, we need to mute the array and then assign a new value.

Syntax to mute the array :

let mut array_name:[data_type; size_of_array] = [data_1,data_2,data_3,...data_n];

Syntax to assign a new value referring to an array index :

 array_name[index] = new_value;

Program to modify a value of an array :

fn main() {
 let mut my_array: [i32;5] = [1, 2, 3, 4, 5];
 println!("Before modification the array was: {:?}", my_array);
 my_array[2] = 30;
 println!("After modification the array is {:?}", my_array);
}

In the above program, we have seen that the array my_array is initialized with five values- 1,2,3,4 and 5. Now after the formal initialization we have reinitialized the index 2. (my_array[2] = 30;) And so the third element in the array which was 3 is now updated to 30.

Program Output :

rust-array-modify

Array as a parameter



Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions