Skip to content
Xen / docs

Search documentation

English · v0.1.0-demo.2

Type to search the documentation.

    한국어
    Xen / reference v0.1.0-demo.2

    std.hashmap

    APIs documented for this release.

    use std.hashmap;

    Use std.hashmap.HashMap<V> to look up values by string keys. Save this as hashmap.xen:

    use std.hashmap as hashmap;
    #![explc]
    fn main() {
    let mut prices = hashmap.new<Int>();
    prices.insert("tea", 3);
    prices.insert("coffee", 4);
    let key = "tea";
    println(match prices.get_cloned(&key) {
    Option.Some(price) => price,
    Option.None => 0,
    });
    println(prices.len());
    }
    Terminal window
    $ xen run hashmap.xen
    3
    2
    TaskAPI
    Createstd.hashmap.new<V>()
    Count or check emptinessmap.len(), map.is_empty()
    Insert or replacemap.insert(key, value); returns the previous value as Some if present
    Look upmap.get_cloned(&key); returns a copy in an Option
    Check presencemap.contains_key(&key)
    Removemap.remove(&key); returns the removed value in an Option
    List keysmap.keys(); an independent Vec<String>
    Emptymap.clear()
    Consume and iteratemap.into_iter(); (String, V) items

    The map itself is move-only. get_cloned cannot retrieve noncopyable values such as Box or File. Take them out with remove, or inspect them through std.hashmap.with_value<V, R>(&map, &key, inspect) using a named fn(&V) -> R callback. Explicit reference arguments require explc. Key lists and iteration have unspecified order. Use the APIs rather than changing map fields directly.