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
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
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=3Key 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;orlet [x, y, z] = array;.
Practice what you learned
1. Which type signature describes a fixed-size array of 5 integers?
2. How do you access the second element of a tuple `t = (25, 5.9, 'M')`?
3. What is the key difference between a tuple and an array in Rust?
4. What does the expression `[0; 3]` produce?
5. Can the length of an array change after it is created?
Was this page helpful?
You May Also Like
Structs in Rust
Structs let you group related values into a single named type with fields you access by name.
Vectors in Rust
A Vec<T> is a growable, heap-allocated list that lets you store a variable number of values of the same type.
Slices in Rust
Slices are references to a contiguous sequence of elements within a collection, letting you view data without owning it.
The String Type in Rust
Rust's owned, growable, heap-allocated String type versus the borrowed &str string slice, and how to build and combine them.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics