Tuples and destructuring
Quick answer
A tuple groups a fixed number of values, potentially of different types. It is useful for returning several closely related results without defining a named record.
Tuple types and patterns
The type (i32, bool) contains an integer followed by a boolean. Access a field by its zero-based position (.0, .1) or use let (number, valid) = pair; to destructure it into bindings. A one-element tuple requires a comma: (7,).
fn main() { let measurement = (24, true); println!("{} {}", measurement.0, measurement.1);}Expected stdout (not executed):
24 trueThe first field is an integer; the second is a boolean.
fn divide(n: i32, d: i32) -> (i32, i32) { (n / d, n % d) }fn main() { let (quotient, remainder) = divide(17, 5); println!("{quotient}, {remainder}");}Expected stdout (not executed):
3, 2The divisor is a known nonzero positive value in this example. Destructuring names both results.
Common mistake
Do not confuse (7) with (7,): the first is just a grouped integer.
Try it yourself
Return the minimum and maximum of two small integers in a tuple; destructure the result.
Static examples for Rust 1.85.0, edition 2024. Run them locally in a disposable project; ByteMind execution support and these outputs have not been verified.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Tuples and destructuring — practice quiz
Choose one answer per question, then review the explanation.
Which syntax creates a one-element tuple?
Given `let data = (false, 17);`, what is data.0?
What is y after `let (_, y) = (3, 8);`?