avatarAnkit Tanna

Summary

The provided content discusses the use of Arrays in Rust, highlighting their iterability, fixed length, and same-type element storage, distinguishing them from Tuples and Structs.

Abstract

The content delves into the concept of Arrays in the Rust programming language, emphasizing their role as a means to store a collection of values of the same type. Unlike Tuples and Structs, Arrays in Rust are iterable, which is a significant distinction. The article explains how to define and manipulate Arrays, noting their fixed length determined at compile time. It also demonstrates array indexing, destructuring, and iteration using the .iter() method. The author points out that the uniformity of element types in Arrays allows Rust to ensure safe operations during iteration, which is not possible with Tuples or Structs due to their potential for mixed value types. The article concludes with an invitation for reader feedback and a call to subscribe to a newsletter for more information on Rust.

Opinions

  • The author suggests that Rust's approach to Arrays is unique compared to other programming languages, particularly regarding compile-time length determination and runtime safety.
  • The iterability of Arrays is presented as a key advantage over Tuples and Structs, with the implication that this feature enhances Rust's safety and concurrency guarantees.
  • The article implies that Rust's strict typing and compile-time checks contribute to its reliability and predictability in runtime behavior, which is beneficial for developers.
  • By providing code examples and observations, the author conveys a positive opinion about the clarity and straightforwardness of working with Arrays in Rust.
  • The inclusion of a newsletter subscription link and a request for feedback indicates the author's interest in engaging with the Rust community and fostering continuous learning and improvement.
The Rust Programming Language

The Rust Programming Language — Collections — Arrays

Just like Tuples and Structs, Array is another way you can store collection of values in a sequence. The only difference between Tuples/Structs and an Array in Rust is that Arrays are iterable where as Tuples and Structs are not iterable. Another significant difference is that Tuples and Structs can be a collection of different kinds of values where as an Array has to be a collection of same type of value.

let x: (i32, i32, bool) = (2001, 2023, false);

Arrays in Rust are quite different than any other languages that you have used. Refer below snippet to understand how you can define Arrays:

fn main() {
    let mut years: [i32; 3] = [2021, 2022, 2023];
}

Observations:

  1. You need to declare the type and length of the array using [i32; 3].
  2. You can have a mutable or a non-mutable array.

The above array says that all of the elements have the type of i32. Another difference as compared to other languages is that Arrays in Rust have a hardcoded length at compile time.

Array has very similar features to what we did with Tuples and Struct. It can access value using index. It can be de-structured. Refer below code snippet:

fn main() {
    let mut years: [i32; 3] = [2021, 2022, 2023];
    let first_year = years[0];
    let [_, second_year, third_year] = years;
    let x = 1;
    let year_by_index = years[x];

    // years[3] <-- compile time panic
    // years[x] <-- run time panic if x > 2

    years[2] = 2024;
}

Observations:

  1. Arrays are fixed length at compile time
  2. If you try to access beyond the indexes available it will throw a compile time error.
  3. If you pass x to years it will not throw a compile time error because Rust does not know what x is. Hence it throws a run time error when x exceeds 2.

You can also iterate over years. An array provides an iterable function called iter() which can be used to iterate over the array.

fn main() {
    let mut years: [i32; 3] = [2021, 2022, 2023];

    for year in years.iter() {
        println!("Year: {}", year);
    }
}

Observations:

  1. You don’t need unwanted () around the for loop like other languages
  2. You need to access the iterable object of an array using .iter() method.

Why is Array iterable and not Tuple or Structs? Well, going back to the top where we see that Array has same type of elements, Rust can be confident about the operations that can happen for each iteration (say adding 1 to each number in an array). Hence, Arrays are iterable where as Tuples and Booleans are not.

That’s it for Arrays in The Rust Programming Language. Please share your feedback in the comments section.

You can subscribe to my newsletter about The Rust Programming Language here. You can read about all the articles in this series here.

Rust
Rustlang
Programming
Programming Languages
Webassembly
Recommended from ReadMedium