Rust Tuples and Arrays

Tuples and arrays group several values under one name. A tuple works like a mixed lunch box with different foods in each compartment. An array works like an egg carton with identical slots in a fixed row. Both live directly on the stack, so access stays fast.

Tuples

A tuple holds a fixed number of values, and each value may have a different type. Round brackets wrap the items.

let person = ("Asha", 29, 5.6);
 person
 +--------+----+-----+
 | "Asha" | 29 | 5.6 |
 +--------+----+-----+
    .0      .1    .2

Reading Tuple Values

A dot followed by the position number reads one item. Counting begins at zero.

println!("Name: {}", person.0);
println!("Age: {}", person.1);

Destructuring

Destructuring splits a tuple into separate variables in one line. The code stays tidy when you need every item.

let (name, age, height) = person;
println!("{name} is {age} years old");

Returning Several Values

A function returns only one value, yet a tuple packs several results into that single value.

fn min_max(a: i32, b: i32) -> (i32, i32) {
    if a < b { (a, b) } else { (b, a) }
}

fn main() {
    let (low, high) = min_max(9, 4);
    println!("{low} {high}");
}
4 9

The Unit Type

An empty tuple () is called the unit type. Functions that return nothing actually return unit.

Arrays

An array stores values of one type, and its length never changes. Square brackets wrap the items. The type notation [i32; 4] means four integers.

let days: [i32; 4] = [10, 20, 30, 40];
 index:   0    1    2    3
        +----+----+----+----+
 days   | 10 | 20 | 30 | 40 |
        +----+----+----+----+

Accessing Items

Square brackets with an index read an item. The len() method reports the length.

println!("{}", days[2]);     // 30
println!("{}", days.len());  // 4

Repeating a Value

Use a semicolon form to fill an array with one value. The code below creates ten zeros.

let zeros = [0; 10];

Looping Through an Array

let fruits = ["apple", "mango", "pear"];
for fruit in fruits {
    println!("{fruit}");
}

Bounds Checking

Rust verifies every index at run time. Reading days[10] from a four-item array stops the program with a panic message instead of reading random memory. Many older languages allow that mistake, and it causes security problems. Rust blocks it by design.

let index = 10;
// days[index] would panic: index out of bounds
if let Some(v) = days.get(index) {
    println!("{v}");
} else {
    println!("No item at that position");
}

The get method returns an Option, so you can handle a missing item calmly.

Tuples Compared with Arrays

FeatureTupleArray
Item typesMixedIdentical
LengthFixedFixed
Access styleperson.0days[0]
Loop with forNoYes

When to Use Which

Choose a tuple for a small bundle of related facts, such as a name and an age. Choose an array for a list with a known size, such as the twelve month names. Pick a vector when the list must grow or shrink.

Common Mistakes

Starting the count at one instead of zero causes off-by-one errors. The first item of an array lives at index 0, and the last lives at length - 1.

Trying to grow an array fails, because the length is part of the type. Arrays of type [i32; 3] and [i32; 4] are different types. Switch to a vector when the number of items changes.

Printing a tuple with {} fails. Tuples lack a friendly display form, so use {:?}.

Practice Ideas

  • Store the seven weekday names in an array and print each with its position.
  • Write a function that returns the smallest and largest item of an array as a tuple.
  • Create a tuple holding a product name, a price, and a stock count, then destructure it.

Key Points

  • Tuples mix types and use dot numbers for access.
  • Arrays hold one type and use square-bracket indexes.
  • Both have a size fixed at compile time.
  • Rust checks array indexes and stops unsafe access.