Skip to content
Theory · Variables and Types · lesson 11 of 12

Tuples and arrays

So far, each box has held one value. But values often come in groups: a person's name together with their age and height, the weather for three days. For such groups Rust has two compound types.

Picture a box with compartments, like a lunch box: a sandwich in one compartment, an apple in another, juice in the third. This is a tuple: several values of different types in one variable. And a row of identical boxes numbered in order is an array: several values of one type.

Rust
fn main() {
    let person = ("Anna", 30, 1.68);
    println!("{} {}", person.0, person.1); // Anna 30
    println!("{person:?}"); // ("Anna", 30, 1.68)
}

Let's go line by line:

  1. ("Anna", 30, 1.68) is a tuple of three values: a string, a whole number and a fractional one.
  2. person.0 and person.1 get elements by their position, after a dot. Positions start at zero.
  3. {} cannot print a tuple, while {:?} shows it whole, in parentheses.

A tuple

How many elements a tuple has and what their types are is fixed when it is created and never changes. There are two ways to get an element out.

By position

person.2 takes the third element, 1.68. The position is written in the code as a number, and it cannot be chosen while the program runs.

Destructuring into variables

You can take a tuple apart with one let: on the left, in parentheses, you list names, and each gets the element from its own place. The _ sign takes the place of an element you do not need:

Rust
let (name, _, height) = ("Anna", 30, 2.0);
println!("{name} {height}"); // Anna 2
println!("{height:?}"); // 2.0
  1. name gets the first element, "Anna".
  2. _ takes the second place: the number 30 is thrown away, but the third place still goes to height.
  3. height gets 2.0. As in the lesson about numbers, {} prints it as 2, and {:?} prints it as 2.0.

An array

An array holds several values of one type in a row. It is written in square brackets:

Rust
let days = ["mon", "tue", "wed"];
let zeros = [0; 4];
println!("{} {}", days[0], days.len()); // mon 3
println!("{days:?} {zeros:?}"); // ["mon", "tue", "wed"] [0, 0, 0, 0]
  1. days is an array of three strings.
  2. [0; 4] is an array of four zeros: first the value, then the count.
  3. days[0] takes an element by its position in square brackets, and days.len() returns the length of the array, 3.
  4. The whole array is printed with {:?}. With {} the compiler answers error[E0277]: `[&str; 3]` doesn't implement `std::fmt::Display` . &str in the message is what the compiler calls the type of text in double quotes, and [&str; 3] is an array of three such strings.

The type of an array is written as [i32; 3]: the type of the elements and their count. The length is part of the type, so you cannot add a fourth element to such an array. The position of an element has the type usize.

Here the analogy parts with real life: real boxes are numbered from one, while the elements of an array or a tuple are numbered from zero. The first element of days is days[0], and the last is days[2].

Interviewers ask how a tuple differs from an array. The answer:

  • a tuple holds values of different types, an array holds values of one type;
  • a tuple element is taken after a dot, person.0, and an array element in square brackets, days[0];
  • the size of both is known at build time and does not change.

Common mistake

The element taken has a position equal to the array's length:

Rust
fn main() {
    let days = ["mon", "tue", "wed"];
    let last = days.len();
    println!("{}", days[last]); // ✗
}
TEXT
index out of bounds: the len is 3 but the index is 3

There are three elements, and their positions are 0, 1 and 2: the array has no position 3. Rust checks the position on every access and, if there is no such element, stops the program with a panic instead of returning a random value. The last element is days[days.len() - 1]. And if the wrong position is visible right in the code, as in days[3], the compiler stops the program before it runs: error: this operation will panic at runtime.

Key points

  • A tuple holds values of different types; elements are taken with .0, .1 or by destructuring, let (a, _, c) = ….
  • An array [i32; 3] holds values of one type; the length is part of the type and does not change.
  • Positions start at zero, and an access past the end of an array is a panic.
  • Tuples and arrays are printed with {:?}.