Rust - Array

Last Updated : 9 Jun, 2026

Rust arrays are fixed-size collections that store multiple values of the same type in contiguous memory. They are written using the syntax [T; N], where T is the data type and N is the number of elements. Arrays are useful when the size of the collection is known in advance.

Example: Creating and Printing an Array

Rust
fn main() {
    let arr = [1, 2, 3, 4];
    println!("{:?}", arr);
}

Output
[1, 2, 3, 4]

Syntax

Arrays in Rust are represented using the following syntax:

[DataType; Size]

  • DataType: Type of elements stored in the array.
  • Size: Number of elements in the array.

An array is written as:

let arr: [i32; 3] = [4, 5, 6];

Here:

  • i32 is the data type of the elements.
  • 3 is the size of the array.
  • [4, 5, 6] are the array elements.

Features of an Array

  1. Arrays store elements of the same data type in contiguous memory locations.
  2. Arrays have a fixed size, which means their length cannot be changed after creation.
  3. Each element is accessed using its index, starting from 0.
  4. Array elements can be updated or modified using their indexes.
  5. Arrays provide fast access to elements because indexing is direct.

Declaring and Initializing Arrays

Arrays can be declared and initialized in different ways depending on whether you want Rust to infer the type, specify the type manually or create repeated values.

Syntax 1: Type Inference

let variable_name = [value1, value2, value3];

Rust automatically determines the array data type from the provided values.

Syntax 2: Explicit Data Type and Size

let variable_name: [data_type; array_size] = [value1, value2, value3];

Here:

  • data_type specifies the type of elements.
  • array_size specifies the fixed size of the array.

Syntax 3: Array with Repeated Values

let variable_name: [data_type; array_size] = [default_value; array_size];

This creates an array where all elements are initialized with the same value.

Printing Elements of a Simple Array

Array elements can be printed using println!("{:?}", array) for the entire array or by looping through the array elements one by one. The len() method is used to get the size of the array.

Example 1: Printing Array Elements Using Loop

Rust
fn main() {
    let arr = [0, 1, 2, 3, 4];

    for x in &arr {
        print!("{} ", x);
    }
}

Output
0 1 2 3 4 

Example 2: Printing Entire Array and Size

Rust
fn main() {
    let arr: [i32; 5] = [1, 2, 3, 4, 5];

    println!("Array is {:?}", arr);
    println!("Array size is {}", arr.len());
}

Output
Array is [1, 2, 3, 4, 5]
Array size is 5

Working with Arrays without Data Type

Rust can automatically infer the data type of an array from its elements, so explicitly specifying the type is optional in many cases. The size of the array can still be accessed using the len() method.

Rust
fn main() {
    let arr = [1, 2, 3, 4, 5];

    println!("Array: {:?}", arr);
    println!("Array size: {}", arr.len());
}

Output
Array: [1, 2, 3, 4, 5]
Array size: 5

Explanation:

  • let arr = [1, 2, 3, 4, 5]; creates an integer array without explicitly mentioning the data type.
  • Rust automatically infers the type as [i32; 5].
  • arr.len() returns the total number of elements in the array.

Array Default Values

Rust allows creating an array where all elements are initialized with the same default value using the [value; size] syntax.

Rust
fn main() {
    let arr: [i32; 5] = [0; 5];

    println!("Array: {:?}", arr);
    println!("Array size: {}", arr.len());
}

Output
Array: [0, 0, 0, 0, 0]
Array size: 5

Explanation:

  • [0; 5] creates an array with 5 elements, where each element is initialized to 0.
  • arr.len() returns the number of elements in the array.

Working with Loops on Array

Arrays can be traversed using loops to access each element along with its index. In Rust, indexing starts from 0.

Rust
fn main() {
    let arr: [i32; 5] = [1, 2, 3, 4, 5];

    for index in 0..5 {
        println!("Index: {} Value: {}", index, arr[index]);
    }
}

Output
Index: 0 Value: 1
Index: 1 Value: 2
Index: 2 Value: 3
Index: 3 Value: 4
Index: 4 Value: 5

Explanation:

  • 0..5 generates values from 0 to 4.
  • arr[index] accesses the element at the given index.
  • The loop prints both the index and its corresponding array value.

Using iter() Function

The iter() method is used to iterate through all elements of an array one by one without using indexes.

Rust
fn main() {
    let arr: [i32; 5] = [1, 2, 3, 4, 5];

    for val in arr.iter() {
        println!("Value: {}", val);
    }
}

Output
Value: 1
Value: 2
Value: 3
Value: 4
Value: 5

Explanation:

  • arr.iter() returns an iterator over the array elements.
  • The for loop accesses each element one by one.
  • val stores the current element during each iteration.

Mutable Array

By default, arrays in Rust are immutable. To modify array elements, the array must be declared using the mut keyword.

Rust
fn main() {
    let mut arr: [i32; 5] = [1, 2, 3, 4, 5];

    arr[1] = 0;

    println!("{:?}", arr);
}

Output
[1, 0, 3, 4, 5]

Explanation:

  • mut makes the array mutable.
  • arr[1] = 0 updates the element at index 1.
  • The updated array is printed using println!("{:?}", arr).

Multidimensional Arrays

Rust supports multidimensional arrays by storing arrays inside another array. This is useful for representing matrices or tabular data.

Example: 2D Array in Rust

Rust
fn main() {
    let arr = [
        [1, 2, 3],
        [4, 5, 6]
    ];

    println!("{:?}", arr);
    println!("{}", arr[0][1]);
}

Output
[[1, 2, 3], [4, 5, 6]]
2

Explanation:

  • arr[0] accesses the first inner array.
  • arr[0][1] accesses the element at row 0 and column 1.
  • All inner arrays must have the same size in a fixed Rust array.

Passing Arrays as Parameters to Functions

Arrays can be passed to functions either by value or by reference. Passing by value creates a copy of the array, while passing by reference allows modifying the original array.

Example 1: Pass by value

Rust
fn main() {
    let arr = [1, 2, 3, 4];

    change_array(arr);

    println!("Original array {:?}", arr);
}

fn change_array(mut arr: [i32; 4]) {
    for i in 0..4 {
        arr[i] = 0;
    }

    println!("Changed array {:?}", arr);
}

Output
Changed array [0, 0, 0, 0]
Original array [1, 2, 3, 4]

Explanation:

  • The array is copied when passed to change_array(arr).
  • Changes inside the function do not affect the original array.

Example 2: Pass by reference

Rust
fn main() {
    let mut arr = [1, 2, 3, 4, 5];

    change_array(&mut arr);

    println!("Original array {:?}", arr);
}

fn change_array(arr: &mut [i32; 5]) {
    for i in 0..5 {
        arr[i] = 0;
    }

    println!("Changed array {:?}", arr);
}

Output
Changed array [0, 0, 0, 0, 0]
Original array [0, 0, 0, 0, 0]

Explanation:

  • &mut arr passes a mutable reference to the function.
  • Changes made inside the function directly modify the original array.
Comment