Rust Lang

Rust Get Array Length

In Rust, an array refers to a collection of similar data types that is stored in contiguous memory blocks. In Rust, the arrays are fixed-size and have a specific length which is determined by the Rust compiler during the compilation stage.

In order to work with Rust arrays effectively, it is often necessary to know the array length beforehand.

The goal of this tutorial is to explore how to get the length of an array in Rust. We will cover two main ways: the “len” method and the “size_of_val” functions.

At the end of this tutorial, you will clearly understand how to get the length of an array in Rust and which method to use depending on your specific use case. Let’s get started!

Create an Array in Rust

The simplest and most common method of creating an array in Rust is using the square bracket notation.

For example, the following code creates an array of five integer elements:

let my_array = [1, 2, 3, 4, 5];

 
We can also create an array with repeated values using the “;” syntax. An example demonstration is shown as follows:

let my_array = [0; 10];

 
In this example, we create the “my_array” which contains ten initialized elements with 0. The first value inside the square brackets denotes the initial value. The second parameter specifies the length of the array.

Method 1: Rust Len Method

We can use the len() method to determine the length of a given array. However, it is good to keep in mind that the length of the array refers to the number of elements in the specified array.

The function syntax is as follows:

array.len()

 
This method does not accept any parameters.

fn main() {
    let my_array = [1, 2, 3, 4, 5];
    let array_length = my_array.len();
    println!("The length of the array is: {}", array_length);
}

 
This should determine the length of the specified array as shown in the following:

The length of the array is: 5

 
In the previous example, we create an array called “my_array” with five elements and then use the “len” method to get its length. The “len” method returns a value of usize type which represents the array’s number of elements. We then print the length of the array to the console using “println! macro”.

Method 2: Using the Std::mem:size_of_val Function

Another method of fetching the length of an array in Rust is using the “std::mem::size_of_val” function.

The function returns the size of the value in bytes. Hence, to get the length of the array using this function, we can multiply the size of one element by the total number of elements in the array.

Consider the following example demonstration:

fn main() {
    let my_array = [1, 2, 3, 4, 5];
    let array_length = std::mem::size_of_val(&my_array) / std::mem::size_of_val(&my_array[0]);
    println!("The length of the array is: {}", array_length);
}

 
In the previous example, we divide the size of the entire array by the size of one element to get the number of elements in the array.

Finally, we print the array length to the console using “println!”.

Conclusion

You now discovered how to use the len() and size_of_val() functions to determine the length of an array in Rust. Although both methods work similarly, the “size_of_val” function tends to be a little inefficient compared to the len() function. This is because it requires an extra calculation which may not be effective in an array with non-standard memory layouts.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list