Arrays in Rust

Introduction to Arrays in Rust

Welcome back! Today, we're diving into arrays in Rust. In our last lesson, we explored tuples, a way to group different types of data into a single compound type. Arrays are somewhat similar but come with their own unique set of characteristics and benefits.

An array in Rust is a collection of elements of the same type, stored in a contiguous block of memory. This can be especially useful when you have a fixed-size collection of elements that you need to manage efficiently. Unlike tuples, every element of an array must be the same data type.

Imagine a scenario where you need to store the temperatures recorded for each day of the week. An array can be an ideal candidate for this. Let's explore arrays in Rust in more detail!

Creating Arrays in Rust

There are two ways to declare arrays in Rust:

  1. With explicit type and length: You specify both the data type of elements and the array's size.
  2. With type inference: You let Rust figure out both the data type and length based on the values you provide.

When declaring an array explicitly, you need to specify the data type of elements, include a semicolon followed by the array's size, and place both inside square brackets (like [i32; 4]). You must also ensure all values match the declared type. When using type inference, you simply provide the values in square brackets, and Rust automatically determines both the type and length for you.

Here's how you can create arrays with and without explicit type and length declarations:

fn main() {
    // Creating Array with explicit Data Type and Length
    let array_with_type: [i32; 4] = [1, 2, 3, 4]; 
    // Creating Array without explicit Type or Length (compiler infers both)
    let array_without_type = [5, 6, 7, 8];

    println!("Array with type: {:?}", array_with_type); // Prints: [1, 2, 3, 4]
    println!("Array without type: {:?}", array_without_type); // Prints: [5, 6, 7, 8]
}

In the code above:

  • array_with_type is an array explicitly typed as having four elements, all of type i32.
  • array_without_type relies on type inference to determine both the elements' type and the array's length (4 in this case).

Recall, to print the values of an array, we use {:?} in the println! statement.

Accessing Elements of an Array

In Rust, you can access the elements of an array using index notation. Arrays are zero-indexed, meaning the first element of the array is at index 0. To access the value of an array, use the array name followed by square brackets containing the index number. Here’s how you can do it:

fn main() {
    let array = [1, 2, 3, 4];
    println!("First element: {}", array[0]); // Prints: First element: 1
    println!("Fourth element: {}", array[3]); // Prints: Fourth element: 4
}

In this example:

  • We accessed the first element using array[0].
  • We accessed the fourth element using array[3].
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal