Rust Guide > Documentation > Collections > Array

Introduction

The Array class in Rust represents a fixed-size collection of elements of the same type. Arrays are useful for scenarios where you know the size of the collection at compile time and need efficient, indexed access to elements.

Using Array

Arrays in Rust can be created using a fixed-size syntax. Here is a basic example:

fn main() {
    let arr = [1, 2, 3, 4, 5];
    println!("Array: {:?}", arr);
}
// Output: Array: [1, 2, 3, 4, 5]

Key Methods

Below are some of the key methods exposed by the Array class:

len

Returns the number of elements in the array.

fn main() {
    let arr = [1, 2, 3, 4, 5];
    println!("Length: {}", arr.len());
}
// Output: Length: 5

is_empty

Returns true if the array is empty.

fn main() {
    let arr: [i32; 0] = [];
    println!("Is empty: {}", arr.is_empty());
}
// Output: Is empty: true

get

Returns an option containing a reference to the element at the specified index, or None if the index is out of bounds.

fn main() {
    let arr = [1, 2, 3, 4, 5];
    match arr.get(2) {
        Some(&val) => println!("Value at index 2: {}", val),
        None => println!("No value at index 2"),
    }
}
// Output: Value at index 2: 3

iter

Returns an iterator over the array's elements.

fn main() {
    let arr = [1, 2, 3, 4, 5];
    for val in arr.iter() {
        println!("{}", val);
    }
}
// Output:
// 1
// 2
// 3
// 4
// 5

as_slice

Returns a slice containing the entire array.

fn main() {
    let arr = [1, 2, 3, 4, 5];
    let slice = arr.as_slice();
    println!("Slice: {:?}", slice);
}
// Output: Slice: [1, 2, 3, 4, 5]

Example Usage

Example 1: Basic Usage

fn main() {
    let arr = [1, 2, 3, 4, 5];
    println!("Array: {:?}", arr);
}
// Output: Array: [1, 2, 3, 4, 5]

Example 2: Getting Array Length

fn main() {
    let arr = [1, 2, 3, 4, 5];
    println!("Length: {}", arr.len());
}
// Output: Length: 5

Example 3: Checking if Array is Empty

fn main() {
    let arr: [i32; 0] = [];
    println!("Is empty: {}", arr.is_empty());
}
// Output: Is empty: true

Example 4: Accessing Elements Safely

fn main() {
    let arr = [1, 2, 3, 4, 5];
    match arr.get(2) {
        Some(&val) => println!("Value at index 2: {}", val),
        None => println!("No value at index 2"),
    }
}
// Output: Value at index 2: 3

Example 5: Iterating Over Elements

fn main() {
    let arr = [1, 2, 3, 4, 5];
    for val in arr.iter() {
        println!("{}", val);
    }
}
// Output:
// 1
// 2
// 3
// 4
// 5

Example 6: Converting to Slice

fn main() {
    let arr = [1, 2, 3, 4, 5];
    let slice = arr.as_slice();
    println!("Slice: {:?}", slice);
}
// Output: Slice: [1, 2, 3, 4, 5]

Considerations

  • Arrays in Rust have a fixed size known at compile time. For dynamically sized collections, use Vec.
  • Array indexing starts at 0, meaning the first element is accessed with index 0.
  • Be careful with indexing to avoid out-of-bounds errors, which will cause a panic at runtime.

See Also

  • Vec - A growable, dynamically-sized collection.
  • Slice - A dynamically-sized view into a contiguous sequence of elements.
  • Option - An enum that can contain either a value or None, useful for safe indexing.