Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A tuple in Rust is a finite, heterogeneous, compound data type that groups multiple values into a single construct. Tuples possess a fixed arity (length) established at compile time; they cannot dynamically grow or shrink. Because they are heterogeneous, each position within a tuple has a distinct, statically verified type.

Syntax and Type Signatures

Tuples are constructed using a comma-separated list of expressions enclosed in parentheses. The type signature of a tuple mirrors its value syntax. To create a single-element tuple (a 1-tuple), a trailing comma is strictly required in both the value and the type signature. This trailing comma is necessary for the compiler to distinguish a 1-tuple from a standard parenthesized expression.
// Implicit type inference
let inferred_tuple = (500, 6.4, 'z');

// Explicit type annotation
let explicit_tuple: (i32, f64, char) = (500, 6.4, 'z');

// Single-element tuple (1-tuple) requires a trailing comma
let single_element: (i32,) = (5,);

// Without the comma, this evaluates to a simple integer, not a tuple
let not_a_tuple: i32 = (5);

Element Access

Tuple elements are accessed via two primary mechanisms: direct indexing and pattern matching (destructuring). Direct Indexing Elements are accessed using dot notation followed by the zero-based index of the value. Unlike arrays, tuple indices must be constant literals, not variables, because the type of each element is resolved at compile time.
let data: (i32, f64, u8) = (500, 6.4, 1);

let five_hundred = data.0;
let six_point_four = data.1;
let one = data.2;
Destructuring Tuples can be unpacked into distinct variables using pattern matching.
let data = (500, 6.4, 1);
let (x, y, z) = data;

// Ignoring specific elements using the wildcard `_`
let (just_x, _, _) = data;

Mutability

A tuple inherits its mutability from its binding. If a tuple is declared as mutable using the mut keyword, its individual elements can be mutated in place, provided the new values strictly match the original positional types.
let mut state = (0, false);

// Valid mutation
state.0 = 1;
state.1 = true;

// Compilation error: mismatched types
// state.0 = "string"; 

The Unit Type

A tuple with an arity of zero is a special case in Rust known as the unit type. Its value and its type are both represented by empty parentheses (). It represents an empty value or an expression that evaluates to nothing, serving a role similar to void in C or C++.
let unit_value: () = ();

Trait Implementation Limits

Because Rust currently lacks variadic generics, the standard library implements common traits (such as Debug, Clone, PartialEq, Eq, and Default) for tuples only up to an arity of 12. While the compiler permits the creation of tuples with more than 12 elements, these larger tuples do not automatically inherit standard library trait implementations. Consequently, attempting to print a 13-element tuple using the {:?} formatter or comparing two 13-element tuples using == will result in a compilation error.

Memory Layout

Tuples are stored as contiguous blocks of memory. However, Rust does not guarantee the order of elements in memory for standard tuples. The compiler may reorder the internal layout of a tuple to minimize padding and optimize memory alignment.
Master Rust with Deep Grasping Methodology!Learn More