In Rust, a tuple is a fixed-size group of values that can be of different types. You define a tuple using parentheses, like let person = (“Alice”, 30). Tuples are useful when you want to return or store multiple values together without creating a full struct.
Tuples are one of the simplest ways to group different values in Rust. If you are coming from JavaScript, you might be used to using arrays or objects to store multiple values. In Rust, tuples give you a lightweight way to group values without defining a full struct or custom type.
In this post, I will explain what tuples are, how to create them, access their values, and use them in real code. If you want to return more than one value from a function, tuples are your first tool.
What Is a Tuple?
A tuple is a group of values inside parentheses:
let person = ("Alice", 30, true);
This tuple contains a &str, an i32, and a bool. Tuples are ordered and indexed, and they can contain different types, unlike arrays.
Tuples have a fixed length. Once created, you cannot add or remove values from them.
Accessing Tuple Values
Each element in a tuple is accessed by position, starting from zero:
let person = ("Alice", 30, true);
println!("Name: {}", person.0);
println!("Age: {}", person.1);
println!("Is Active: {}", person.2);
You use dot notation with the index. This works even when the values are of different types.
Destructuring a Tuple
You can also “unpack” a tuple into individual variables:
let person = ("Bob", 25, false);
let (name, age, is_active) = person;
println!("{} is {} years old. Active? {}", name, age, is_active);
This is called destructuring, and it is useful when you want to give each value a meaningful name.
You can also ignore values with an underscore:
let (_, age, _) = person;
println!("Age is {}", age);
Tuples in Functions
Tuples are a common way to return multiple values from a function.
Example:
fn calculate_area(width: i32, height: i32) -> (i32, i32) {
let area = width * height;
let perimeter = 2 * (width + height);
(area, perimeter)
}
fn main() {
let (a, p) = calculate_area(5, 10);
println!("Area: {}, Perimeter: {}", a, p);
}
This shows how functions can return grouped results without needing a struct.
Tuple Types
Tuples have a type based on the number and type of elements:
let t: (i32, f64, char) = (42, 6.5, 'z');
You do not often need to write the type explicitly unless the compiler needs help or you are writing a function signature.
Empty Tuple or Unit Type
Rust also has a special empty tuple: (), called the unit type. It has only one value: ().
This is often used as:
- The return type of functions that return “nothing”
- A placeholder when no real value is needed
Example:
fn do_nothing() {
// returns ()
}
If you write let x = (), then x holds the unit value.
Summary
Tuples in Rust are a lightweight way to group multiple values of different types. You can access their elements by position, destructure them into individual variables, and use them in functions to return more than one value. Tuples are fixed in size and commonly used in early-stage Rust programming for grouped values.
Next up: Arrays and Slices in Rust