Array functionality is still very basic as you cannot resize arrays (incl. pushing elements) at runtime. You can only get, set, and replace array elements. Future releases of Mun will extend this functionality.

Dynamically Sized Arrays

In Mun, the default array type is dynamically sized and heap-allocated. To create an array, write its values as a comma-separated list inside square brackets, as shown in Listing 3-1.

let array = [1, 2, 3, 4, 5];

Listing 3-1: Creating an array instance

An array's type is written using square brackets around the type of each element, as demonstrated in Listing 3-2. This is only necessary when the compiler cannot automatically deduce the type of the array based on the context; although you you can always manually notate your code.

let array: [u64] = [1, 2, 3, 4, 5];

Listing 3-2: Creating an array instance of type [u64]

Accessing Array Elements

An array is a single chunk of heap-allocated memory of dynamic size. You can access elements of an array using indexing, as illustrated in Listing 3-3.

fn main() {
    let array = [1, 2, 3, 4, 5];

    let first = array[0];
    let second = array[1];

Listing 3-3: Accessing elements of an array instance`

In this example, the variable named first will get the value 1, because that is the value at index [0] in the array. The variable named second will get the value 2 from index [1] in the array.

Invalid Array Element Access


Mun is still in early development and should thus be considered unsafe! In particular for arrays, as they're allowed to perform out-of-bounds memory access.

Currently, Mun does not check for invalid array element access. As such, it will attempt to access an array element, even if it is out of bounds. You will have to manually prevent out-of-bounds access; e.g. as shown in Listing 3-4.

pub fn generate() -> [u64] {
    [5, 4, 3, 2, 1]

pub fn add_one(array: [u64], len: usize) -> [u64] {
    let idx = 0;
    loop {
        array[idx] += 1;
        idx += 1;

        if idx >= len {
            break array

fn main() {
    add_one(generate(), 5);

Listing 3-4: Preventing invalid element access of an array instance

When Mun implements a way to panic, this will change.