Rust - Array - GeeksforGeeks

31 Mar, 2021
AN Array in Rust programming is a fixed-sized collection of elements denoted by [T; N] where is T is the element type and N is the compile-time constant size of the array.
We can create an array in 2 different ways:
Simply a list with each element [a, b, c].Repeat expression [N, X].  This will create an array with N copies of X.
Simply a list with each element [a, b, c].
Repeat expression [N, X].  This will create an array with N copies of X.
[X,0] it is allowed but can cause some hectic problems so if you are using this type of expression be mindful of side effects. Array size from 0-32 implements the default trait if allowed by its type. Trait implementations are up to 32 sizes. An array is not iterateable itself.
Syntax:
ArrayType : [ Type ; Expression ]
An array is written as:
let array: [i32; 3] = [4, 5, 6];
The features of an array are listed below :
Memory blocks in the array are in sequential order.
Once an array is created it cannot be resized which means the array is static.
The array element is represented by the memory block
To identify array elements we use a unique integer known as a subscript.
Putting values in the array element is known as array initialization.
Values in the array element can be updated or modified but cannot be deleted.
The below-given surtaxes can be used to declare and initialize an array in Rust:
Syntax1: let variable_name = [value_1,value_2,value_3];

Syntax2: let variable_name:[dataType;array_size] = [value_1,value_2,value_3];

Syntax3: let variable_name:[dataType;array_size] = [default_value_for_elements,array_size];
The value of the datatype is getting from the data type of the first variable of the array.
 To print all values in the array use the {:?} syntax of the println!() function. To compute the size of the array we can use the len() function.
Example 1: Printing Elements in the array
Rust
#![allow(unused)]fn main() {let mut array: [i32; 5] = [0; 5];  array[1] = 1;array[2] = 2;array[3] = 3;array[4] = 4;assert_eq!([1, 2 , 3 ,4], &array[1..]);  // This loop prints: 0 1 2 3 4for x in &array {   print!("{} ", x);}}
Output: 
0 1 2 3 4
Example 2: Printing Elements in the array
Rust
fn main(){  let arr:[i32;5] = [1,2,3,4,5];  println!("array is {:?}",arr);  println!("array size is :{}",arr.len());}
Output:
array is [1, 2, 3, 4, 5]
array size is :5
The program below declares an array of 5 elements. Here we are not explicitly defining the data type during variable declaration. So, the array will be of type integer. To compute the size of the array we can use the len() function.
Rust
fn main(){  let arr = [1,2,3,4,5];  println!("array is {:?}",arr);  println!("array size is :{}",arr.len());}
Output:
array is [1, 2, 3, 4, 5]
array size is :5
Let us create an array and initialize all the values with the default value 0.
Rust
fn main() {  let gfg_array:[i32;5] = [0;5];  println!("array is {:?}",gfg_array);  println!("array size is :{}",gfg_array.len());}
Output:
array is [0, 0, 0, 0, 0]
array size is :5
The following example iterates through an array and prints the indexes and their corresponding values. The loop retrieves values from index 0 to 4 (index of the last array element).
In this example, an array is iterated by a loop and prints the index and the value.  Loop retrieves the value from 0 to 5
Rust
fn main(){  let gfg_array:[i32;5] = [1,2,3,4,5];  println!("array is {:?}",gfg_array);  println!("array size is :{}",gfg_array.len());    for index in 0..5 {     println!("index is: {} & value is : {}",index,gfg_array[index]);  }}
Output:
array is [1, 2, 3, 4, 5]
array size is :5
index is: 0 & value is : 1
index is: 1 & value is : 2
index is: 2 & value is : 3
index is: 3 & value is : 4
index is: 4 & value is : 5
The iter() function fetches values of all elements in an array.
Rust
fn main(){  let arr:[i32;5] = [1,2,3,4,5];  println!("array is {:?}",arr);  println!("array size is :{}",arr.len());    for val in arr.iter(){     println!("value is :{}",val);  }}
Output:
array is [1, 2, 3, 4, 5]
array size is :5
index is: 0 & value is : 1
index is: 1 & value is : 2
index is: 2 & value is : 3
index is: 3 & value is : 4
index is: 4 & value is : 5
To declare the mutable array we use “mut” key. Mutable arrays are those arrays whose elements can be altered. See the example below for a better understanding.
Rust
fn main(){  let mut arr:[i32;5] = [1,2,3,4,5];  arr[1] = 0;  println!("{:?}",arr);}
Output:
[1, 0, 3, 4, 5]
We have updated the value of the array at index number “1” and print the value.
We can pass an array as a parameter either by a call by value or call by reference to a function.
Example 1: Pass by value
Rust
fn main() {  let gfg_array = [1,2,3,4];  change_array(gfg_array);    print!("Original array {:?}",gfg_array);}fn change_array(mut gfg_array:[i32;4]){  for i in 0..4 {     gfg_array[i] = 0;  }  println!("Changed array {:?}",gfg_array);}
Output:
Changed array [0, 0, 0, 0]
Original array [1, 2, 3, 4]
Example 2: Pass by reference
Rust
#![allow(unused)]  fn main() {  let mut gfg_array = [1,2,3,4,5];  change_array(&mut gfg_array);  print!("Original array {:?}",gfg_array);}fn change_array(gfg_array:&mut [i32;5]){  for i in 0..5 {     gfg_array[i] = 0;  }  println!("Changed array {:?}",gfg_array);}
Output:
Changed array [0, 0, 0, 0, 0]
Original array [0, 0, 0, 0, 0]
Picked
Rust-basics
Rust
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Generate Random Numbers in Rust Language?
Rust - Casting
Rust - For and Range
Rust - Generic Function
Rust - Drop Trait
Rust - Creating a Library
Rust - References & Borrowing
Rust - Strings
Rust - Concept of Structures
Rust - Box Smart Pointer