Array In Rust

Introduction

Arrays are a fundamental data structure in many programming languages, and Rust is no exception. In Rust, arrays provide a fixed-size collection of elements that are stored contiguously in memory. Unlike dynamic data structures like vectors, the size of an array is determined at compile-time and remains constant throughout its lifetime. In this article, we will take a deep dive into arrays in Rust, covering their declaration, accessing elements, array length, iteration, mutability, and more.

Declaration and Initialization

To declare an array in Rust, we specify the type and the number of elements enclosed in square brackets. Here's an example:

let array: [i32; 5]; // Declares an array of 5 elements of type i32

We can also initialize the array with specific values using the following syntax:

let array = [1, 2, 3, 4, 5]; // Initializes the array with specific values

Accessing Elements

Array elements in Rust can be accessed using their index, which starts from 0.

Example

fn main() {
    let array = [1, 2, 3];
    let first_element = array[0]; // Accesses the first element (1)
    let second_element = array[1]; // Accesses the second element (2)
    println!("First Element : {}", first_element);
    println!("Second Element : {}", second_element);
}

Output

Array Element Access

Example 2

fn main() {
    let array = ["Uday","Bhavesh","Jeet"];
    let first_element = array[0]; // Accesses the first element (Uday)
    let second_element = array[1]; // Accesses the second element (Bhavesh)
    println!("First Element : {}", first_element);
    println!("Second Element : {}", second_element);
}

String Array Element Access

Note. If we try to access an element with an invalid index, it will give a run-time error.

Array Length

To obtain the length of an array, we can use the len() method, which returns the number of elements in the array:

Example

fn main() {
    let array1 = [1,2,3,4,5];
    let array2 = ["Uday","Bhavesh","Jeet"];

    println!("Length of Array 1 : {}", array1.len()); // Returns the length of the array (5)
    println!("Length of Array 2 : {}", array2.len()); // Returns the length of the array (3)
}

Output

Array Length

Iterating over an Array

Iterating over the elements of an array can be done using a for loop or the iter() method. Here's an example of both approaches.

Example

fn main() {
    // Using for loop
    println!("Using For Loop : ");
    let array = [1, 2, 3, 4, 5];
    for element in &array {
        println!("{}", element);
    }

    // Using iter()
    println!("Using iter() Method : ");
    for element in array.iter() {
        println!("{}", element);
    }
}

Output

Array Itrerate

Array Slicing

Array slicing allows us to create a slice that references a subset of an array's elements. Slicing is done using the range operator (..). Consider the following example.

Example

fn main() {
    let array = [1, 2, 3, 4, 5];
    let slice = &array[1..3];

    println!("After Slice Value Of Array is : ");
    for element in slice {
        println!("{}", element);
    }
}

Output

Array Slicing

Array Mutability

By default, arrays in Rust are immutable. However, if you need to modify the elements of an array, you can use the mut keyword:

Example

fn main() {
    let mut array = [1, 2, 3];
    array[0] = 10;
    println!("{}", array[0]);
}

Output

Array Mutable

If we try to change the value of a un mutable array, then it's a five array.

Example

fn main() {
    let array = [1, 2, 3];
    array[0] = 10;
    println!("{}", array[0]);
}

Output

Array Mutable Error

Summary

Arrays are a powerful and efficient data structure in Rust that provide a fixed-size collection of elements. They offer predictable memory layouts and performance advantages in scenarios where the size of the collection is known and does not change. In this article, we explored the declaration, accessing elements, array length, iteration, mutability, and slicing of arrays in Rust. Understanding arrays and their features is essential for building efficient and low-level systems in Rust.


Similar Articles