In this article, we will explore how we can work with arrays in the Rust programming language.
Rust Create Array
In Rust, we create an array using a pair of square brackets in the format as shown below:
Where T represents the type of elements to store in the array and N represents the size of the array. Remember that the size refers to the number of elements an array can hold.
Rust provides us with two principal methods for creating an array:
- An array with a list of elements during declaration.
- Repeat expression where a specified element is repeated a specified number of items. The syntax of the repeat array is as shown [N, X]. This creates an array of the elements x repeated n times.
Rust Declare and Initialize Array
There are three ways we can declare and initialize arrays in Rust.
Now, let us discuss:
1. Array Without Data Type or Size
The first method of declaring an array is creating an array without a data type or size. An example syntax is provided below:
The previous example creates an array without a type or size. To get the length of the array, you can use the built-in len() function.
let arr = [1,2,3,4,5];
println!("Length of the array {}", arr.len());
}
The previous example should return the length of the array as:
Length of the array 5
2. Array With Data Type and Size
The next method of declaring an array is by setting its type and the size. An example syntax for declaring such an array is as shown below:
The previous syntax creates an array of 32-bit signed integers with a length of 5. Printing the length of the array should return 5.
// 5
3. Array With Default Value
You can also create an array where all the elements of an element take a pre-defined default value.
The syntax is as shown:
An example is as shown:
The previous example creates an array of type &str with the size of 5. Then, we set a default value as “a” for all elements in the array.
Rust Print Array
To print an array in Rust, we use the 😕 Operator inside the println! function.
An example is provided below:
let arr:[&str; 5] = ["a"; 5];
println!("Array {:?}", arr);
}
The previous code prints the elements of the array as provided below:
Rust Iterate Array
We can iterate over the index and elements of an array using a for loop or the iter function. Now, let us discuss.
For Loop Iteration
The most common method of iterating an array is to use a for loop. An example code is as shown below:
let arr:[i32; 5] = [1,2,3,4,5];
for index in 0..arr.len() {
println!("index: {}, value: {}", index, arr[index]);
}
}
The previous example uses a loop to iterate the index of array from index 0 to the length of the array.
Remember that indexing starts at index 0 in Rust.
To access the item stored at a specific index, we use the name of the array and pass the index we wish to access inside a pair of square brackets.
Running the previous code should return:
index: 1, value: 2
index: 2, value: 3
index: 3, value: 4
index: 4, value: 5
Rust Iter Method
We can also use the iter method to the elements in an array. An example is provided below:
let arr:[i32; 5] = [1,2,3,4,5];
for value in arr.iter() {
println!("{}", value);
}
}
The previous code should print each element in the array as shown below:
--- truncated ---
5
Mutable Arrays
By default, the arrays are immutable. This means that you cannot change the elements once defined. To create a mutable array, you can add the mut keyword during array declaration.
An example is as shown:
Declaring an array using the previous syntax allows you to change elements in the array.
Conclusion
In this article, we explored how we can declare and use arrays in the Rust programming language. There are three ways to declare and initialize the arrays, which include an array without data type and size, an array with data type and size, and an array with default value. We hope you found this article helpful. Check the other Linux Hint articles for more tips and information.