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 aHashMapwhen the count is in the low tens and the keys need noHashimpl. - Multi-return without naming a type.
function divmod(...) -> Pair<i64, i64>. - Map entries.
Pairis the element type of a map's entries.Iterator::zip,Iterator::enumerateandOption::zipyield 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
| Property | Type |
|---|---|
first | A |
second | B |
Constructors
static new(first: A, second: B) -> Pair<A, B>;
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
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct Pair<A, B> with [Eq, Clone] {
first: A;
second: B;
}Two values held together, first and second: a map's key with its value.
type parameter Atype parameter Btype struct Point with [Eq, Clone, Debug] { .. }Asks the compiler to generate implementations of the listed traits for the type, each working through its fields or variants in declaration order. The derivable traits are Eq, Ord, Hash, Clone, Debug, Default, ToValue and FromValue, and the text formats' FromJson, ToJson, FromYaml, ToYaml, FromToml and ToToml. The clause comes after the name and its parameters, before where.
type trait Eq<Rhs = This> {
equals(&this, other: &Rhs) -> boolean;
}Types that support value equality with a value of Rhs, which is the type itself unless an implementation names another: implement trait Eq for Str compares two Strs, and implement trait Eq<string> for Str a Str with a string.
type trait Clone {
clone(&this) -> This;
}A type whose values can be duplicated into independent copies. A type takes it with with [Clone] on its declaration, or by implementing clone by hand.
Pair.first: Atype parameter APair.second: Btype parameter Bimport a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct Pair<A, B> with [Eq, Clone] {
first: A;
second: B;
}Two values held together, first and second: a map's key with its value.
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
function divide(n: i64, d: i64) -> Pair<i64, i64>(parameter) n: i64(parameter) d: i64return value;Leaves the current function with the given value. A function whose return type is not void must return one; a bare return; is for void. In an async function it completes the future with the value.
static Pair::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.
std::core::pair(parameter) n: i64(parameter) d: i64function main() -> intconst name: Type = value;An immutable binding: it cannot be reassigned after initialisation. The annotation may be omitted when an initialiser is present - the binding takes the initialiser's concrete type, inferred locally. Mutability is opt-in through mut.
const qr: Pair<i64, i64>function divide(n: i64, d: i64) -> Pair<i64, i64>const qr: Pair<i64, i64>Pair.first: APair.second: Bvalue as TypeExplicit conversion: between numeric types, or reinterpreting one pointer type as another. Cryo never converts implicitly, and as inserts no range checks - a narrowing cast is the programmer's responsibility.
type struct HashMap<K, V, A = GlobalAllocator, H = SipHasher> where A: Allocator {}A map from keys to values, found by the key's hash. Keys need Hash and Eq. H is the hasher: SipHasher by default, which resists keys chosen to collide; FnvHasher is faster for trusted keys. Iteration order is unspecified; BTreeMap keeps keys sorted.
type trait Hash {
hash<H>(&this, hasher: mut &H) -> void
where H: Hasher;
}A type whose values can be hashed, so they can key a HashMap or sit in a HashSet. Values that are equal under Eq must feed the same bytes. A type takes it with with [Hash] on its declaration, or by implementing hash by hand.
function divmod(...) -> Pair<i64, i64>function name(count: i32, args...)A variadic parameter: the trailing bucket receives any number of further arguments with C's calling convention, so the function stays ABI-compatible with the printf family. Wrap the bucket in VariadicArguments to read typed values from it.
type trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> i64;
fold<Acc, F>(mut &this, initial: Acc, f: F) -> Acc
where F: (Acc, This::Item) -> Acc;
for_each<F>(mut &this, f: F) -> void
where F: (This::Item) -> void;
take(this, n: i64) -> implement Iterator<This::Item>;
skip(this, n: i64) -> implement Iterator<This::Item>;
map<B, F>(this, f: F) -> implement Iterator<B>
where F: (This::Item) -> B;
filter<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
chain<J>(this, other: J) -> implement Iterator<This::Item>
where J: Iterator<This::Item>;
enumerate(this) -> implement Iterator<(i64, This::Item)>;
zip<J>(this, other: J) -> implement Iterator<(This::Item, J::Item)>
where J: Iterator;
collect<C>(this) -> C
where C: FromIterator<This::Item>;
sum<S>(this) -> S
where S: Sum, This: Iterator<S>;
reverse(this) -> implement Iterator<This::Item>
where This: DoubleEndedIterator;
peekable(this) -> implement Peekable<This::Item>;
flat_map<B, F>(this, f: F) -> implement Iterator<B::Item>
where F: (This::Item) -> B, B: Iterator;
flatten(this) -> implement Iterator<This::Item::Item>
where This::Item: Iterator;
take_while<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
skip_while<P>(this, pred: P) -> implement Iterator<This::Item>
where P: (&This::Item) -> boolean;
step_by(this, step: i64) -> implement Iterator<This::Item>;
filter_map<B, F>(this, f: F) -> implement Iterator<B>
where F: (This::Item) -> Option<B>;
any<P>(mut &this, pred: P) -> boolean
where P: (This::Item) -> boolean;
all<P>(mut &this, pred: P) -> boolean
where P: (This::Item) -> boolean;
find<P>(mut &this, pred: P) -> Option<This::Item>
where P: (&This::Item) -> boolean;
min(mut &this) -> Option<This::Item>
where This::Item: Ord;
max(mut &this) -> Option<This::Item>
where This::Item: Ord;
min_by<F>(mut &this, compare: F) -> Option<This::Item>
where F: (&This::Item, &This::Item) -> Ordering;
max_by<F>(mut &this, compare: F) -> Option<This::Item>
where F: (&This::Item, &This::Item) -> Ordering;
min_by_key<K, F>(mut &this, key: F) -> Option<This::Item>
where F: (&This::Item) -> K, K: Ord;
max_by_key<K, F>(mut &this, key: F) -> Option<This::Item>
where F: (&This::Item) -> K, K: Ord;
last(mut &this) -> Option<This::Item>;
nth(mut &this, n: i64) -> Option<This::Item>;
position<P>(mut &this, pred: P) -> Option<i64>
where P: (This::Item) -> boolean;
product<S>(this) -> S
where S: Product, This: Iterator<S>;
}A source of values handed out one at a time by next, until it returns None. for (x in source) drives one, and the adapter methods below build new iterators from it:
Iterator::zip<J>(this, other: J) -> implement Iterator<(This::Item, J::Item)>
where J: IteratorYield the tuple (a, b) drawing a from this and b from other, stopping as soon as either iterator is exhausted. A loop takes the tuple apart: for ((a, b) in xs.iterate().zip(ys.iterate())).
Iterator::enumerate(this) -> implement Iterator<(i64, This::Item)>Yield each element with its index, as the tuple (index, element), the index counting up from 0 as an i64. A loop takes the tuple apart: for ((i, x) in xs.iterate().enumerate()).
type enum Option<T> with [Eq, Clone, Debug] {
Some(T);
None;
}A value of type T that may be absent: Some(value) or None.
Option::zip<U>(this, other: Option<U>) -> Option<(T, U)>Both values as a tuple, this option's first, when both options are Some; None otherwise, releasing whichever value there was.
type trait Copy {}Types whose values can be duplicated by a bitwise copy. Integers, floats, pointers, and pure-POD structs qualify; anything that owns resources (heap storage, file handles, refcount) does not.
std::core::markerstatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
(parameter) first: A(parameter) second: Bimplement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
type parameter Awhere A: Display
type parameter Bwhere B: Display
type trait Name : Base {
method(&this, other: &This) -> Ret;
}Names a set of methods a type may implement. Inside the body This is the implementing type; methods may carry default bodies, and a trait may require a base trait (type trait Ord : Eq). Generic code asks for a trait with where T: Name.
type trait Display {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
to_string(&this) -> String;
}How a value reads to a person: what {x} in an f-string and println write. Implement it for a type a user sees.
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language referencewhere T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type parameter Awhere A: Debug
type parameter Bwhere B: Debug
type trait Debug {
fmt(&this, f: mut &Formatter) -> Result<(), FormatError>;
}How a value reads to a programmer: what {x:?} writes, and {x:#?} over several lines. with [Debug] on a declaration writes it field by field.