Skip to content
Systems programming for what’s next.
— Vex

Tuples ​

Tuples group multiple values of possibly different types into a single compound value. They are fixed-size, ordered, and heterogeneous.

Syntax and Construction ​

vex
// Parenthesized comma-separated values
let pair = (1, "hello")
let triple = (true, 3.14, 'x')

// Explicit type annotation
let coord: (f64, f64) = (10.0, 20.0)

// Nested tuples
let nested = ((1, 2), "three", (4.0, 5.0))

The Unit Type: () ​

The empty tuple () is Vex's unit type -- a zero-size type with exactly one value. It represents "no meaningful value" and is the default return type of functions that don't return anything.

vex
// Function returning nothing
fn doSomething() {
    // Implicitly returns ()
}

// Explicitly
fn explicitUnit(): () {
    return ()
}

Tuple Field Access ​

Access tuple elements by position using dot-index syntax:

vex
let pair = (42, "answer")

let num: i32 = pair.0     // 42
let text: string = pair.1  // "answer"

// Mutable tuple fields
let! mut = (1, 2)
mut.0 = 99
mut.1 = 100

Destructuring ​

Tuples can be destructured in let bindings and match arms:

vex
let point = (10, 20)

// Destructuring let
let (x, y) = point
// x = 10, y = 20

// Destructure tuple arguments inside the function
fn distanceSquared(first: (f64, f64), second: (f64, f64)): f64 {
    let (x1, y1) = first
    let (x2, y2) = second
    let dx = x2 - x1
    let dy = y2 - y1
    return dx * dx + dy * dy
}

let squared = distanceSquared((0.0, 0.0), (3.0, 4.0))  // 25.0

let, match, and for support nested tuple patterns. Successive bindings can make a deeply nested shape easier to read.

Destructuring does not erase reference lifetimes. Returning a selected reference retains the lifetime of that element, rather than automatically borrowing every sibling. Returning a reference to a local owner is still rejected. Alternative (|) patterns conservatively retain all possible source lifetimes: the returned reference must remain valid for whichever alternative matches.

Pattern Matching with Tuples ​

Tuples are first-class patterns in match expressions:

vex
fn axis(point: (i32, i32)): str {
    return match point {
        (0, 0) => "origin",
        (0, _) => "y-axis",
        (_, 0) => "x-axis",
        (_, _) => "neither axis",
    };
}
fn main(): i32 {
    $println(axis((0, -2)));
    $println(axis((-2, 0)));
    $println(axis((0, 0)));
    $println(axis((3, 4)));
    return 0;
}

The program prints y-axis, x-axis, origin, and neither axis, with exit code 0. The first coordinate is x and the second is y.

Nested tuple patterns are also supported in match:

vex
let nested = ((1, 2), 3);
let total = match nested { ((a, b), c) => a + b + c };

Returning Multiple Values ​

Since functions return a single value, tuples are the idiomatic way to return multiple values:

vex
fn divideAndRemainder(a: i32, b: i32): (i32, i32) {
    return (a / b, a % b)
}

let (quotient, remainder) = divideAndRemainder(17, 5)
// quotient = 3, remainder = 2

Tuple Comparison ​

Tuples support equality comparisons when their corresponding element types support Eq:

vex
let a = (1, "abc")
let b = (1, "abc")
let c = (2, "xyz")

let same = a == b    // true: every corresponding element is equal
let diff = a == c    // false

Equality compares corresponding elements. Tuple ordering (<, >, <=, >=) is not provided; write an explicit comparison policy when an algorithm needs lexicographic or domain-specific ordering.

Copy and Move Semantics ​

A tuple is Copy if ALL its element types are Copy:

vex
// All primitive types are Copy -> tuple is Copy
let a = (1, 2.0, true)
let b = a    // Copy, a still usable

// Contains Box (non-Copy) -> tuple is NOT Copy
let c = (1, Box.new(42))
let d = c    // Move, c is consumed

Tuples vs Structs ​

FeatureTupleStruct
Field access.0, .1, .2 position-based.fieldName name-based
Type identity(T1, T2) structuralNamed type
Best forQuick, ad-hoc groupingMeaningful, reusable data
Destructuringlet (a, b) = tlet S { x, y } = s or a match pattern

Use tuples for short-lived, positionally-meaningful groupings (return values, coordinate pairs). Use structs when fields have semantic meaning and the type is reused.

Tuple Arity ​

Use () for unit and pairs, triples, or larger tuples for positional grouping. Use a named wrapper when one field needs a distinct domain identity. Prefer a named struct once larger tuples become difficult to distinguish by position.

vex
// 0-element: unit
let nothing = ()

// 2-element: pair
let pair = (1, "a")

// 3-element: triple
let triple = (1, 2, 3)

// 4+ element
let quad = (1, 2.0, true, 'x')

Released under the MIT License.