100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Rust

Tuples and Arrays in Rust

Tuples group a fixed set of differently-typed values together, while arrays hold a fixed-size list of same-typed values.

Data StructuresBeginner8 min readJul 8, 2026
Analogies

Introduction

Tuples and arrays are both fixed-size compound types in Rust, but they solve different problems. A tuple groups together a fixed number of values of potentially different types, such as (i32, f64, char). An array holds a fixed number of values of the same type, such as [i32; 5], where the size is part of the type itself and known at compile time. Neither can grow or shrink, unlike Vec<T>.

🏏

Cricket analogy: A tuple is like a scorecard entry (runs, balls, fours) mixing different stat types for one player, while an array is like a fixed XI batting order of 11 same-type player slots — neither can add a 12th player mid-match.

Syntax

rust
let person: (i32, f64, char) = (25, 5.9, 'M');

let numbers: [i32; 5] = [1, 2, 3, 4, 5];
let zeros = [0; 3]; // [0, 0, 0]

Explanation

The tuple person holds three values of different types in one fixed-size unit; you access its elements with .0, .1, and .2, or destructure it directly with let (age, height, gender) = person;. Arrays like numbers hold values of a single type, and their length is fixed and encoded in the type [i32; 5] — you cannot push or pop elements. The shorthand [0; 3] creates an array of three elements all initialized to 0. Both tuples and arrays support destructuring in pattern matching, letting you bind their elements to individual variables in one step, e.g. let [a, b, c] = numbers_of_three;.

🏏

Cricket analogy: The tuple person is like a player's profile card (age, height, gender) where you pull individual fields with .0, .1, .2 or destructure it at selection time; the array numbers is like a fixed 5-over powerplay schedule you can't extend, and [0; 3] is like pre-filling three empty scoreboard slots with zero.

Example

rust
fn main() {
    let person: (i32, f64, char) = (25, 5.9, 'M');
    let (age, height, gender) = person;
    println!("age={} height={} gender={}", age, height, gender);

    let numbers: [i32; 5] = [10, 20, 30, 40, 50];
    println!("first={} last={}", numbers[0], numbers[4]);
    println!("length={}", numbers.len());

    let [a, b, c] = [1, 2, 3];
    println!("a={} b={} c={}", a, b, c);
}

// Output:
// age=25 height=5.9 gender=M
// first=10 last=50
// length=5
// a=1 b=2 c=3

Key Takeaways

  • Tuples hold a fixed number of values that can have different types.
  • Arrays hold a fixed number of values that must all share the same type.
  • An array's length is part of its type, e.g. [i32; 5], and is known at compile time.
  • Tuple elements are accessed with .0, .1, etc.; array elements with [index].
  • Both support destructuring, e.g. let (a, b) = tuple; or let [x, y, z] = array;.

Practice what you learned

Was this page helpful?

Topics covered

#Rust#RustProgrammingStudyNotes#Programming#TuplesAndArraysInRust#Tuples#Arrays#Syntax#Explanation#DataStructures#StudyNotes#SkillVeris