Tuples & Arrays
Tuples and arrays are Rust's two fixed-size compound types — a tuple bundles a set number of values of different types, while an array holds a fixed-length run of values that all share one type.
Learn Tuples & Arrays in our free Rust course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Rust course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
In this lesson you'll destructure tuples, index and iterate arrays, understand bounds safety, and see how arrays relate to slices and the growable Vec .
What You'll Learn in This Lesson
1️⃣ Tuples: Grouping Mixed Values
A tuple packs a fixed number of values, each possibly a different type, into one value. You read its fields by position — .0 , .1 , .2 — or, more idiomatically, you destructure it into named variables.
The empty tuple () is special: it's the unit type , Rust's way of saying "no meaningful value." Functions that return nothing actually return () . Tuples are perfect for returning a couple of values from a function without defining a whole struct.
2️⃣ Arrays: Fixed-Length, Same Type
An array's type is [T; N] — element type T and a compile-time length N that never changes. You index from zero, ask its length with .len() , and iterate elements directly. The [value; count] shorthand fills an array with a repeated value.
3️⃣ Slices and the Growable Vec
A slice ( &[T] ) borrows a contiguous part of an array or Vec using a range like &data[1..4] (the end index is exclusive). When you need a collection that can grow at runtime, reach for Vec<T> — a heap-backed, resizable array you build with push or the vec! macro.
Your turn. Fill in the blanks marked ___ , then run it.
Loop over an array of tuples, destructure each one, and track an average and a maximum. Run it with cargo run and check the output.
📋 Quick Reference — Tuples & Arrays
Practice quiz
How do you access the second field of a tuple t?
Tuples are accessed by position with dot syntax: t.0, t.1, t.2.
What is the type signature of an array of three i32 values?
An array type is written [T; N] with element type then length.
Can a single tuple hold values of different types?
- No, all elements must match
- Only if they are numbers
- Only two values
- Yes, a tuple groups mixed types
Answer: Yes, a tuple groups mixed types. A tuple like ("Ada", 36, 1.65) bundles different types together.
Must all elements of an array share one type?
- No, any types allowed
- Yes, arrays are same-type
- Only the first two
- Only references
Answer: Yes, arrays are same-type. An array [T; N] holds a fixed run of values that all have type T.
What does the [0; 5] expression create?
- An array of five zeros
- A range from 0 to 5
- A tuple of two values
- An empty array
Answer: An array of five zeros. [value; count] fills an array with a repeated value, here five 0s.
Is an array's length fixed at compile time?
- No, it can grow
- Only with push
- Yes, it never changes
- Only on the heap
Answer: Yes, it never changes. Arrays are fixed-length; use a Vec when the size must change at runtime.
What happens when you index an array out of bounds with [i]?
- It returns None
- The program panics
- It reads garbage memory
- It returns 0
Answer: The program panics. Rust checks bounds and panics rather than reading invalid memory.
What does scores.get(99) return when the index is out of range?
- Some(0)
- A panic
- -1
- None
Answer: None. .get(i) returns an Option: None when the index is invalid, Some(&value) otherwise.
What does &data[1..4] produce on an array?
- A copy of the array
- A slice borrowing elements 1, 2, 3
- Elements 1 through 4 inclusive
- A tuple
Answer: A slice borrowing elements 1, 2, 3. A slice borrows a contiguous range; the end index 4 is exclusive.
How do you destructure a tuple t into a and b?
Tuple destructuring uses parentheses: let (a, b) = t;.