Skip to content
CryoCryo home
Stdlibcore

pair

import std::core::pair; · source

Types

Pair<A, B>

type struct Pair<A, B> with [Eq, Clone] {
    first:  A;
    second: B;
}

An owned two-element tuple, the library's one form of a two-value result. It owns both members and drops them in declaration order: first before second. The fields are public; p.first and p.second are the access pattern, and there are no getters.

Use it wherever you would otherwise reach for two parallel arrays or a single-purpose struct:

  • Key-value entries. [Pair<Str, Config>] is a small associative list, cheaper than a HashMap when the count is in the low tens and the keys need no Hash impl.
  • Multi-return without naming a type. function divmod(...) -> Pair<i64, i64>.
  • Map entries. Pair is the element type of a map's entries. Iterator::zip, Iterator::enumerate and Option::zip yield tuples instead, which a tuple pattern takes apart.

Eq and Clone are parametric: a Pair has each when both members do. A Pair has no destructor of its own, so it may be taken apart into its members, and a pair of Copy members is itself Copy.

import std::core::pair::{ Pair };

function divide(n: i64, d: i64) -> Pair<i64, i64> {
    return Pair::new(n / d, n % d);
}

function main() -> int {
    const qr: Pair<i64, i64> = divide(17, 5);
    return (qr.first + qr.second) as int;   // 3 + 2
}

Properties

PropertyType
firstA
secondB

Constructors

static new(first: A, second: B) -> Pair<A, B>;

Construct a pair from its two members. Both are moved in by value; if a member owns heap state, that ownership transfers to the pair and will be released when the pair itself is dropped.

Trait implementations

implement<A, B> trait Display for struct Pair<A, B> where A: Display, B: Display
std::fmt::display
implement<A, B> trait Debug for struct Pair<A, B> where A: Debug, B: Debug
std::fmt::display