MOCKSTACKS
EN
Questions And Answers

More Tutorials








RUST Array

An array is a homogeneous data structure, that stores a sequence of consecutively numbered values--allocated in contiguous memory. Each object of the array can be accessed by using its number (i.e., index).

The features of an array are as listed below:
  • An array declaration allocates sequential memory blocks.
  • Arrays are static. This means that an array once initialized cannot be resized.
  • Each memory block represents an array element.
  • Array elements are identified by a unique integer called the subscript/ index of the
    element.
  • Populating the array elements is known as array initialization.
  • Array element values can be updated or modified but cannot be deleted.

Example of an Array

In this example we will define an array of integers

fn main(){
 let my_first_array:[i32;4] = [1,2,3,4];
 println!("array is {:?}",my_first_array);
 println!("array size is :{}",my_first_array.len());
}

Output

array is [1, 2, 3, 4]
array size is :4


Example of an Array Without Data Types

In this example we will define an array without data type

fn main(){
 let my_first_array = [1,2,3,4];
 println!("array is {:?}",my_first_array);
 println!("array size is :{}",my_first_array.len());
}

Output

array is [1, 2, 3, 4]
array size is :4


Example of creating an array with default values

In this example we will define an array that creates default values.

fn main(){
 let my_first_array = [5;4];
 println!("array is {:?}",my_first_array);
 println!("array size is :{}",my_first_array.len());
}

Output

array is [5, 5, 5, 5]
array size is :4


Example loop through Array using iter() function

In this example we will define an array and loop through all elements using iter()

fn main(){
 let my_first_array = [1,2,3,4];
 println!("array is {:?}",my_first_array);
 println!("array size is :{}",my_first_array.len());
 for index in 0..4 {
  println!("index is: {} & value is : {}", index, my_first_array[index]);
 }
}

Output

array is [1, 2, 3, 4]
array size is :4
index is: 0 & value is : 1
index is: 1 & value is : 2
index is: 2 & value is : 3
index is: 3 & value is : 4


Example loop through Array using iter() function

In this example we will define an array and loop through all elements using iter() function

fn main(){
 let my_first_array = [1,2,3,4];
 println!("array is {:?}",my_first_array);
 println!("array size is :{}",my_first_array.len());
 for my_value in my_first_array.iter(){
  println!("value is :{}",my_value);
 }
}

Output

array is [1, 2, 3, 4]
array size is :4
value is :1
value is :2
value is :3
value is :4



Conclusion

In this page (written and validated by ) you learned about Rust Array . What's Next? If you are interested in completing Rust tutorial, your next topic will be learning about: Rust Array Sorting.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.