Skip to content
CryoCryo home
Stdlibcore

borrow

import std::core::borrow; · source

Looking a stored value up by another form of it.

Types

Borrow<Q>

type trait Borrow<Q> {
    borrow(&this) -> Q;
}

Borrow<Q> says that a value of this type can be read as a Q that compares and hashes as the value itself does. The hashed collections — HashMap and HashSet — take their lookup key as any Q their key type borrows as, so a HashMap<String, V> is asked with a Str, and every key type is still asked with a reference to itself:

import std::collections::hashmap::{ HashMap };

function main() -> int {
    mut ages: HashMap<String, i64> = HashMap::new();
    ages.insert(String::from("ada"), 36);
    const name: Str = "ada";
    const by_view: boolean = ages.get(name).is_some();                   // a Str: no String is made to ask
    const by_key: boolean = ages.get(&String::from("ada")).is_some();
    return if (by_view && by_key) { 0 } else { 1 };
}

An implementation must keep the two forms in step: a == b exactly when a.borrow() == b.borrow(), and a.borrow() hashes and orders as a does. A lookup that breaks this finds nothing, or the wrong entry.

Methods

borrow(&this) -> Q;

This value read as a Q.

Implementors

implement<T> trait Borrow<&T> for T
implement<A> trait Borrow<Str> for struct String<A> where A: Allocator
std::collections::string
implement<A> trait Borrow<string> for struct String<A> where A: Allocator
std::collections::string