An array is an important construct in most programming languages. This post shows how to create one- and two-dimensional arrays in Rust. Although Rust has different ways of creating arrays, the following examples demonstrate some of them.
One-Dimensional Arrays
Before we move to two-dimensional Rust arrays, it is best to revisit one-dimensional arrays. Please check out How to Declare and Initialize an Array. As we already know, we always need to initialize arrays before we use them. For example, the following codes will not compile.
1 2 | let mut my_ints:[i32; 4]; my_ints[0] = 10; |
To fix the compilation, we modify the codes as follows. These new codes will compile successfully.
1 2 | let mut my_ints:[i32; 4] = [0, 0, 0, 0]; my_ints[0] = 10; |
Here is another example of a one-dimensional Rust array. Consider the following codes.
1 2 3 4 5 | let mut my_keywords:[&str; 2] = ["EMPTY"; 2]; for my_keyword in my_keywords.iter() { println!("{}", my_keyword); } |
When we run these codes, we get the following output.
1 2 | EMPTY EMPTY |
One-dimensional arrays are different from two-dimensional arrays. Below is a visual representation of a one-dimensional array. It has one set of indexes for its values, and setting or changing a value requires one index.
If we want to modify the index 0 (zero) value, we could change the above codes to the following.
1 2 3 4 5 6 | let mut my_keywords:[&str; 2] = ["EMPTY"; 2]; my_keywords[0] = "NOT EMPTY NOW"; for my_keyword in my_keywords.iter() { println!("{}", my_keyword); } |
When we run the new codes, we will get the following result.
1 2 | NOT EMPTY NOW EMPTY |
Two-Dimensional Arrays
With one-dimensional arrays, we use one pair of square brackets. However, with two-dimensional arrays in Rust, we use multiple square brackets NOT placed side by side with each other. In Java, we would have something the following. Notice the pairs of square brackets sit side by side with each other Java
1 2 3 4 5 6 7 8 9 | // These are Java codes int[][] myIntMatrix = new int[3][2]; for(int i = 0; i < 3; i++ ) { for(int j = 0; j < 2; j++ ) { myIntMatrix[i][j] = 8; } } |
In Rust, we would have the following equivalent codes to declare and initialize a two-dimensional array. Strange, right? There are nested square brackets!
1 | let my_int_matrix:[[i32;2];3] = [[8;2];3]; |
Therefore, the outer square brackets represent the rows, while the inner square brackets represent the columns of the two-dimensional Rust array. We can visualize the content of the variable my_int_matrix as follows. What if we need three- or n-dimensional arrays? Same drill. We use nested square brackets.
To display the contents of two-dimensional Rust array my_int_matrix :
1 2 3 4 5 6 7 8 | for (i, row) in my_int_matrix.iter().enumerate() { for (j, col) in row.iter().enumerate() { println!("[row={}][col={}]={}", i, j, col); } } // Display value given the indexes println!("{}", my_int_matrix[0][0]); |
Output:
1 2 3 4 5 6 | [row=0][col=0]=8 [row=0][col=1]=8 [row=1][col=0]=8 [row=1][col=1]=8 [row=2][col=0]=8 [row=2][col=1]=8 |
We tested the codes using Rust 1.53.0, and this post is part of the Rust Programming Language For Beginners Tutorial.