pair
import std::collections::pair; · source
Types
Pair<A, B>
type struct Pair<A, B> {
first: A;
second: B;
static new(first: A, second: B) -> Pair<A, B>;
}
An owned two-element tuple. It owns both members and drops them in declaration order. The fields are public; 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>. - Zipping.
Pairis the element type ofIterator::zipandIterator::enumerate, because Cryo has no heterogeneous tuple literal.
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 Clone for struct Pair<A, B> where A: Clone, B: Clone
implement<A, B> trait Drop for struct Pair<A, B>
implement<A, B> trait Eq for struct Pair<A, B> where A: Eq, B: Eq
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
Every impl is parametric: a Pair has it when both members do. Display renders (first, second).
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> {
first: A;
second: B;
}Pair<A, B>: an owned two-element tuple.
type parameter Atype parameter BPair.first: Atype parameter APair.second: Btype parameter Bstatic 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.
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.
(parameter) first: A(parameter) second: Btype struct Pair<A, B> {
first: A;
second: B;
}Pair<A, B>: an owned two-element tuple.
std::collections::pairimport 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 HashMap<K, V, A = GlobalAlloc> where A: Allocator {
buckets: Entry<K, V>**;
bucket_count: u64;
length: u64;
alloc: A;
seed: u64;
}HashMap<K, V, A>: key-value map with amortized O(1) operations.
std::collections::hashmapfunction 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 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 VaArgs to read typed values from it.
type trait Iterator {
type Item;
next(mut &this) -> Option<This::Item>;
count(mut &this) -> u64;
fold<Acc>(mut &this, initial: Acc, f: (Acc, This::Item) -> Acc) -> Acc;
for_each(mut &this, f: (This::Item) -> void) -> void;
take(this, n: u64) -> TakeIter<This>;
skip(this, n: u64) -> SkipIter<This>;
map<B>(this, f: (This::Item) -> B) -> MapIter<This, B>;
filter(this, pred: (&This::Item) -> boolean) -> FilterIter<This>;
chain<J>(this, other: J) -> ChainIter<This, J>;
// … 7 more
}Iterator::zip<J>(this, other: J) -> ZipIter<This, J>Yield pairs (a, b) drawing a from this and b from other, stopping as soon as either iterator is exhausted. The pair is Pair<Item, B> where B is other's element type: p.first comes from this, p.second from other. (Cryo has no heterogeneous tuple literal, so Pair is the element type.)
Iterator::enumerate(this) -> EnumerateIter<This>Yield (index, value) pairs, the index counting up from 0 as a u64. The pair is Pair<u64, Item>: p.first is the index, p.second the element. (Cryo has no heterogeneous tuple literal, so Pair is the element type.)
Pair.first: APair.second: Bstatic 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::collections::pairimplement 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: Clone
type parameter Bwhere B: Clone
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.
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: Eq
type parameter Bwhere B: Eq
type trait Eq {
equals(&this, other: &This) -> boolean;
}Types that support value equality. equals(a, b) must be reflexive, symmetric, and transitive.
type parameter Awhere A: Display
type parameter Bwhere B: Display
type trait Display {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}Display / Debug / Formatter.
std::fmt::displaytype parameter Awhere A: Debug
type parameter Bwhere B: Debug
type trait Debug {
fmt<W>(&this, f: mut &Formatter<W>) -> Result<(), FmtError>
where W: FmtWrite;
}