Updated June 1, 2023
Definition of Rust array
Rust is one of the several programming languages that exist which were developed by Mozilla. This language focus on highly concurrent as well as highly secure systems. Normally, some variables exist to store the values. In the case of arrays, they are considered collections that store values of the same type in contiguous memory locations. Array in Rust is created using the [] square brackets. In addition, it is necessary to know the array size during compile time. If the array size is not defined, it is referred to as a slice. This article will focus on array usage in the Rust programming language.
Syntax:
Below is the syntax of an array in Rust language. There are three ways in which we can create an array. They are:
1. let varname = [ val1 , val2 , val3 ] ;
2. let varname:[ dType ; size ] =[ val1 ,val2 , val3 ] ;
3. let varname:[ dType ; size ] = [def_val, size ] ;
Here, varname is the array name, dType is the data type, and def_val is the default value for elements.
How Rust Array Works?
As we all know, an array sequentially allocates memory blocks that are static. Arrays can be mutable and immutable. In Rust, there are three ways to create an immutable array. Let us see each one of them in detail.
1. Array with no data type
It is possible to create an array without mentioning the data types. Let us see how we can write that. Suppose there are six elements, and we don’t want to mention the data type. Then, we will write the program as shown below.
let arry = [ 13 , 241 , 33 , 24 , 15 , 65 ] ;
2. Array with data type and size
The above example shows an array created without the data types. Now we will see how to create an array with both the data type as well as size. Suppose there are six elements, and elements are 32-bit integers. Then, we will write the program as shown below.
let arry:[i32;6] = [ 13 , 241 , 33 , 24 , 15 , 65 ] ;
3. Default valued array
In addition to the above two types, we can create an array when all the items have only one value. It can be written as shown below.
let arry:[i32;6] = [0;6];
println!("Size of the created array is: {}", arry.len());
}
Now, we will see the case of a mutable array. As the above arrays are immutable, the content cannot be changed. For creating a mutable array in this programming language, the keyword but has to be used, as shown below.
let mut arry:[ i32;6 ] = [ 54 , 65, 76, 87, 98, 90 ];
Examples
So far, we have seen what a Rust array is, what its syntax is, how it works, etc. Now we will see the working of Rust array programs with sample outputs.
Program #1
Program to create a Rust array with no data type
fn main() {
let arry = [ 13 , 241 , 33 , 24 , 15 , 65 ] ;
println!("Created array is {:?}", arry);
println!("Size of the created array is: {}", arry.len()); }
Output:
This program will create an array of six elements with no data type. Furthermore, we utilize the function len() to obtain the size of the array. When the code is executed, it will display both the array elements and the array size, as demonstrated above.
Program #2
Program to create a Rust array with a default value
fn main() {
let arry:[i32;6] = [ 13 , 241 , 33 , 24 , 15 , 65 ] ;
println!("Created array is {:?}", arry);
println!("Size of the created array is: {}", arry.len());
}
Output:
This program creates an array with six elements, specifying both the data type and size. The use of “i32” indicates that the integers in the array are 32 bits, and the array size is 6. In order to verify the size of the array, we utilize the function len(). Executing the code will display both the array elements and the array size, as demonstrated above.
Program #3
Program to create a Rust array with data type and size
fn main() {
let arry:[i32;6] = [0;6];
println!("Size of the created array is: {}", arry.len());
}
Output:
Similar to the previous program, we create an array consisting of six elements with a specified data type and size. In this case, the use of “i32” indicates that the integers in the array are 32 bits, and the array size is 6. In addition to these, a default value of 0 is also provided in the syntax. To verify the size of the array, we utilize the function len(). By executing the code, the array size will be displayed, as demonstrated above.
Program #4
Program to create a Rust mutable array
fn main() {
let mut arry:[i32;6] = [54, 65, 76, 87, 98, 90 ];
println!("Created array: {:?}",arry);
arry[2] = 30;
arry[3] = 40;
arry[4] = 50;
println!("New array: {:?}",arry); }
Output:
In this program, we create a mutable array unlike the previous programs. We use a data type and size to create an array of six elements. Additionally, Rust provides a keyword to indicate that the created array is mutable. In the program, the use of “i32” indicates that the integers in the array are of 32-bits, and the array size is 6. When you execute the code, it will display the array size, as shown above.
Conclusion
To create an array in Rust, you use the square brackets “[]” notation, and it is necessary to know the size of the array during compile time. This article provides a detailed explanation of various aspects, including introduction, syntax, working, and examples of Rust arrays.
Recommended Articles
We hope that this EDUCBA information on “Rust Array” was beneficial to you. You can view EDUCBA’s recommended articles for more information.