Primitive Type

In rust, primitive data types are those, which are built in and do not need any library to be used.1

For instance, signed integers (i8, i32, isize, ...), floating point (f32, f64), char, etc are scalar primitive types as they hold only single values.

On the other hand arrays and tuples are called compound primitive types because they hold multiple values.

Initializing an array

#![allow(unused)]
fn main() {
    // default type is i32
    let arr = [1, 2, 3, 4, 5];
    println!("Array: {:?}", arr);
}

Defining an array

We can specify type and length in the format of [T; N], where:
T: Element Type
N: Size (it is constant and it should be non-negative)

#![allow(unused)]
fn main() {
    // name: [T ; N] = [...
    let arr: [u8; 3] = [0, 3, 5]; 
    println!("Array: {:?}", arr);
}

We can also repeat values by specifying a value to repeat and the number of times to repeat it
[E; N]
E: Expression
N: Number of times to repeat

#![allow(unused)]
fn main() {
    // name: [T ; N] = [E; N];
    let arr: [u8; 4] = [9; 4]; 
    println!("Array: {:?}", arr); 
}

Accessing and modifying an array

We can access array elements using their index, and modify the values provided the array is mutable.

#![allow(unused)]
fn main() {
    // Accessing an element
    let mut arr1_mut = [12, 20, 30, 40];
    let num = arr1_mut[0];
    println!("First element: {}", num);

    // array_name[index] = new_value;
    arr1_mut[0]= 10;
    println!("Modified array 1: {:?}", arr1_mut);
}

Advantages and Disadvantages of Arrays

AdvantagesDisadvantages
Accessing / searching for elements is fast.Fixed size
Can represent multiple elements using single name.Memory wastage
Traversal is easyInsertion and deletion is difficult
Continuous memory allocationSorting is difficult

Types of Array:

1. 1D array:

One dimensional array refers to an array that contains only a row of elements. A 1D array can be accessed by using a single index.

1d array

Initializing a 1D array: Arrays are one dimensional by default. When you initialize an array, like you did at the start, you are initializing a 1D array.

Use Case: 1D arrays are used when we have to store similar items contiguously. These items are related in some way.

2D array:

Two Dimensional array refers to an array that contains rows as well as columns of element. A 2D array can be accessed by using two indices; one index specifying the row, the other specifying the column.

2d array

Notice that the 1D array and 2D array figures have the same 8 elements. The only difference is how they are accessed.

We use only one index to access 4 from 1D: array[2]. On the other hand, we have to use two two values to access 4 from 2D: array[0][2].

In 2D array, we are actually storing arrays in a sequential order, i.e. we are storing an array of arrays. The first index specifies which array we want to access, (0i, 1i), then we specify which element from that array we want to specify, (0j, 1j, 2j, 3j).

Initializing a 2D array: While arrays are stored in a single line, they are ideal for a One dimensional relationship. But in some cases you need to store elements that have a Two dimensional relationship. To structure that data in such a way, this is how we do it:

#![allow(unused)]
fn main() {
    const ROWS: usize = 4;
    const COLS: usize = 5;

    // name: [[ T;    C];    R]
    let arr: [[u8; COLS]; ROWS] = [[1; COLS]; ROWS];
    println!("Array: {:?}", arr); 
}