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 stores 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 directly 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.
:[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 the 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

Arrays can be passed a parameter to a function in the following two ways:

  • Pass By Value
  • Pass By Reference
Pass by Value:

In this method, the array name is directly passed to the function. And inside the function, the data type of the array elements and the length of the array are the two parameters required.

fn function_name(mut array_name:[data_type; length_of_the_array])

A program to update the values of an array using functions:

fn main() {
    let my_array = [10, 20, 30];
    println!("The original array is {:?}", my_array);
    update(my_array);
}
fn update(mut my_array: [i32;3]) {
    for i in 0. .3 {
        my_array[i] = 1;
    }
    println!("After update the array is {:?}", my_array);
}

In the above program, we have simply declared an array called my_array which holds values- 10, 20 and 30. Then we have used the function called update() and passed the array name my_array inside the function.

Now, while defining the update() function, we have written in the following way:

fn update(mut my_array: [i32;3]) 

It means, that the function name is update() is the function name, mut is required, as we are going to change the array contents. And my_array is the parameter name that is going to act as our array. i32 is the data_type of the array elements and 3 is the length of the array.

Program Output :

rust-array-parameters-value

In the program, we have used a for loop which so that the index value can iterate and can be incremented, every time the array is assigned with value 1.

Pass By Reference

In this method, we do not pass the array directly as a function parameter, but we pass the reference of the array variable as the function parameter.

function_name(&mut array_name);

A program to update the value of an array :

fn main() {
    let mut my_array = [1, 2, 3];
    println!("Array values before update {:?}", my_array);
    update( & mut my_array);
}
fn update(my_array: & mut[i32; 3]) {
    for i in 0. .3 {
        my_array[i] = 1;
    }
    println!("After update the value are {:?}", my_array);
}

In the above program, we have declared an array called my_array which has three values inside it- 1,2,3. Then we have passed the reference of the array in the user-defined function called update() using the line- update( & mut my_array);.

Now, when the reference is passed as a function parameter, the reference gets copied into the parameter variable of the update() function.

Program Output :

rust-array-reference

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions