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, there are variables that 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. Moreover, the size of the array should be known during the compile time. If the array size is not defined, then it is known as Slice. In this article, we 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, array allocates memory blocks in a sequential manner and they 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 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
In the above example, we saw 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 of 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 mutable array. As the above arrays are immutable, the content cannot be changed. For creating a mutable array in this programming language, the keyword mut 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 is the syntax, 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:
In this program, an array of six elements will be created with no data type. Moreover, a function len() is also used for obtaining the size of the array. On executing the code, all the array elements and the size of the array will be displayed as shown above.
Program #2: Program to create a Rust array with 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:
In this program, an array of six elements is created with both the data type and size. i32 in the program indicates that integers in the array are 32-bits and array size is 6. The function len() is also used for obtaining the size of the array to verify the same. On executing the code, all the array elements and the size of the array will be displayed as shown 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 above program, an array of six elements is created with both the data type and size. i32 in the program indicates that integers in the array are 32-bits and array size is 6. In addition to these, a default value of 0 is also provided in the syntax. The function len() is also used for obtaining the size of the array to verify the same. On executing the code, the size of the array will be displayed as shown 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:
Unlike the above programs, a mutable array is created in this program. An array of six elements is created with both the data type and size. There is also a keyword but that indicates that the array created is mutable. i32 in the program denotes that integers in the array are of 32-bits and array size is 6. On executing the code, the size of the array will be displayed as shown above.
Conclusion
Array in Rust is created using the [] square brackets and the size of array should be known during the compile time. In this article, different aspects such as introduction, syntax, working, and examples of Rust array are explained in detail.
Recommended Articles
This is a guide to Rust Array. Here we discuss the definition, How array works in Rust, and examples with code implementation. You may also have a look at the following articles to learn more –
  41 Online Courses |  13 Hands-on Projects |  322+ Hours |  Verifiable Certificate of Completion
4.5
View Course
Related Courses