Xen / reference v0.1.0-demo.2
std.hashmap
This topic is not available in the selected language or version. Showing the start of this section.
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());}$ xen run hashmap.xen32| Task | API |
|---|---|
| Create | std.hashmap.new<V>() |
| Count or check emptiness | map.len(), map.is_empty() |
| Insert or replace | map.insert(key, value); returns the previous value as Some if present |
| Look up | map.get_cloned(&key); returns a copy in an Option |
| Check presence | map.contains_key(&key) |
| Remove | map.remove(&key); returns the removed value in an Option |
| List keys | map.keys(); an independent Vec<String> |
| Empty | map.clear() |
| Consume and iterate | map.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.