0

Rust – How to create Two Dimensional Array Example

Rust has a different construct for creating arrays.

One-Dimensional Arrays

Before we move to two-dimensional arrays, it is best to revisit one-dimensional arrays. Please check out How to Declare and Initialize an Array.

Sample Codes

Consider the following codes.

Output:

Our array looks like this visually.

Two-Dimensional Arrays

When creating two-dimensional arrays, we still use multiple square brackets but not placed side by side with each other.

In Java, we would have something like this:

In Rust, we would have:

my_int_matrix is visually represented as:

Therefore, the outer square bracket represents the rows and the inner square bracket represents the columns.

To display the contents of my_int_matrix :

Output:

Karl San Gabriel

Karl San Gabriel

Professional Software Developer