Compound Types: Tuples and Arrays

Updated

October 31, 2026

Compound Types: Tuples and Arrays

Compound types group multiple values into a single type.

Tuples

A tuple has a fixed length and can contain elements of different types:

fn main() {
    let point: (i32, f64, &str) = (10, 3.14, "origin");
    let (x, y, label) = point; // Destructuring
    println!("Point {}: x = {}, y = {}", label, x, y);
    println!("Direct access: first = {}", point.0);
}

Arrays

An array has a fixed length, is stack-allocated, and elements must be of the same type:

fn main() {
    let buffer: [u8; 4] = [0xDE, 0xAD, 0xBE, 0xEF];
    println!("Array length: {}, first: 0x{:X}", buffer.len(), buffer[0]);

    let initialized = [0u32; 16]; // 16 elements initialized to 0
    println!("Initialized array length: {}", initialized.len());
}