Tuples
A tuple groups a fixed number of values — possibly of different types — into a single value, without declaring a named struct. Tuples are written as a parenthesized, comma-separated list in both type position and value position.
let pair: (int, float64) = (1, 2.5);
let triple = ("Rux", 3, true); // (Slice<char8>, int, bool)
When the type can be inferred from the values, the annotation may be omitted, as in triple above.
Type Identity
The number and types of elements are fixed at compile time and are part of the tuple's type. Order is significant, so (int, float64) and (float64, int) are different, incompatible types:
let a: (int, float64) = (1, 2.5);
let b: (float64, int) = a; // error: types do not match
A tuple type carries no field names — only positions — which is what distinguishes it from a struct.
Value Semantics
Like a struct, a tuple is a value type. Assigning it, or passing it to a function, copies every element; the copy and the original are independent.
var p = (1, 2);
var q = p; // full copy
q = (9, 9); // reassigns q only — p is still (1, 2)
Nesting
A tuple element may be any type, including another tuple. This lets you build small structured values without a dedicated type:
let nested: (int, (float64, float64)) = (1, (2.0, 3.0));
Memory Layout
A tuple is laid out like an unnamed struct: its elements are stored in order, each at the next offset that satisfies its natural alignment, with padding inserted where needed. The tuple's alignment is that of its strictest element, and its size is rounded up to a multiple of that alignment.
let t: (uint8, uint32) = (1, 2);
// element 0 at offset 0, element 1 at offset 4 (3 padding bytes between)
// alignment 4, size 8
The only layout difference from a struct is that the elements are reached by position rather than by name.
Further Topics
| Topic | Description |
|---|---|
| Destructuring | Returning several values and unpacking them |
| Tuples vs. Structs | Choosing between positional and named aggregates |
See Also
- Structures — named aggregates with typed fields
- Slices — sequences of a single element type
Variants
A variant can carry data of any type, turning the enum into a type-safe tagged union: the value is always exactly one variant, and the data it holds is determined by which variant that is.
Destructuring
A tuple is most useful when it is produced in one place and unpacked in another. Destructuring binds each element of a tuple to its own name in a single step.