std.dict
The operations module for dictionaries (Dict(K, V)).
use std.dictSemantic Categories
| Category | Function | Behavior |
|---|---|---|
| Read-only borrow | get has values keys entries len is_empty merge | The source dictionary can be reused |
| No arguments | new | Construct an empty dictionary |
| Consumes the source dictionary | set delete | The source dictionary is moved and cannot be used afterward |
use std.assert
use std.dict
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
// Read-only borrow: d can be reused
assert(dict.get(d, "a") == 1)
assert(dict.len(d) == 1)
assert(dict.has(d, "a"))
}Function Overview
| Function | Signature |
|---|---|
get | (K: Type, V: Type)(dict: &Dict(K, V), key: Any) -> Any |
set | (K: Type, V: Type)(dict: Dict(K, V), key: Any, value: Any) -> Dict(K, V) |
has | (K: Type, V: Type)(dict: &Dict(K, V), key: Any) -> Bool |
values | (A: Type, B: Type, C: Type)(dict: &Dict(A, B)) -> Vec(C) |
keys | (A: Type, B: Type, C: Type)(dict: &Dict(A, B)) -> Vec(C) |
entries | (A: Type, B: Type, C: Type)(dict: &Dict(A, B)) -> Vec(C) |
delete | (K: Type, V: Type)(dict: Dict(K, V), key: Any) -> Dict(K, V) |
len | (K: Type, V: Type)(dict: &Dict(K, V)) -> Int |
is_empty | (K: Type, V: Type)(dict: &Dict(K, V)) -> Bool |
merge | (A: Type, B: Type)(a: &Dict(A, B), b: &Dict(A, B)) -> Dict(A, B) |
new | (K: Type, V: Type)() -> Dict(K, V) |
Functions
new
new: (K: Type, V: Type)() -> Dict(K, V)Creates an empty dictionary. K / V are inferred from the context.
You must use this, not
{}:{}is an empty block (its value isVoid), not an empty dictionary (src/std/dict.rs:97-100). The only way to construct an empty dictionary isdict.new(). The dictionary literal{ "k": v, … }can still be used; see Syntax Specification §1.6.4.
use std.assert
use std.dict
main: () -> Void = {
d = dict.new()
assert(dict.is_empty(d))
assert(dict.len(d) == 0)
d2 = dict.set(d, "a", 1)
assert(!dict.is_empty(d2))
}set
set: (K: Type, V: Type)(dict: Dict(K, V), key: Any, value: Any) -> Dict(K, V)Returns a new dictionary with key → value written to it. dict is passed by value and is moved after the call.
use std.assert
use std.dict
main: () -> Void = {
d1 = dict.set(dict.new(), "a", 1)
d2 = dict.set(d1, "b", 2)
assert(dict.len(d2) == 2)
}get
get: (K: Type, V: Type)(dict: &Dict(K, V), key: Any) -> AnyRetrieves the value for a key (read-only borrow; dict can be reused).
Return: The value corresponding to the key. Error: Throws E6008 when the key does not exist (missing key). You can use has to check first.
use std.assert
use std.dict
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
assert(dict.get(d, "a") == 1)
}Retrieve the value after checking for existence:
use std.assert
use std.dict
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
assert(!dict.has(d, "nope"))
if dict.has(d, "a") {
assert(dict.get(d, "a") == 1)
}
}has
has: (K: Type, V: Type)(dict: &Dict(K, V), key: Any) -> BoolWhether key exists in the dictionary.
Error: Throws E6007 when the first argument is not a dictionary.
use std.assert
use std.dict
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
assert(dict.has(d, "a"))
assert(!dict.has(d, "zzz"))
}delete
delete: (K: Type, V: Type)(dict: Dict(K, V), key: Any) -> Dict(K, V)Returns a new dictionary with key removed. dict is passed by value and is moved after the call.
Return: A new dictionary. Deleting a nonexistent key does not cause an error, and the dictionary remains unchanged.
use std.assert
use std.dict
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
deleted = dict.delete(d, "a")
assert(!dict.has(deleted, "a"))
}keys
keys: (A: Type, B: Type, C: Type)(dict: &Dict(A, B)) -> Vec(C)Returns a list of all keys (read-only borrow).
The return order depends on the hash implementation and is not guaranteed to be stable. Sort the results yourself when ordered output is needed.
use std.assert
use std.dict
use std.list
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
ks = dict.keys(d)
assert(list.len(ks) == 1)
}values
values: (A: Type, B: Type, C: Type)(dict: &Dict(A, B)) -> Vec(C)Returns a list of all values (read-only borrow). The order is not guaranteed to be stable.
use std.assert
use std.dict
use std.list
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
vs = dict.values(d)
assert(list.len(vs) == 1)
}entries
entries: (A: Type, B: Type, C: Type)(dict: &Dict(A, B)) -> Vec(C)Returns a list of key-value pairs, with each item being a (key, value) tuple. The order is not guaranteed to be stable.
use std.assert
use std.dict
use std.list
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
es = dict.entries(d)
assert(list.len(es) == 1)
}len
len: (K: Type, V: Type)(dict: &Dict(K, V)) -> IntNumber of entries. Read-only borrow; dict can be reused.
Error: Throws E6007 when the argument is not a dictionary.
use std.assert
use std.dict
main: () -> Void = {
d = dict.set(dict.new(), "a", 1)
assert(dict.len(d) == 1)
assert(dict.len(d) == 1) // Can be reused
}is_empty
is_empty: (K: Type, V: Type)(dict: &Dict(K, V)) -> BoolWhether the dictionary is empty.
Error: Throws E6007 when the argument is not a dictionary.
use std.assert
use std.dict
main: () -> Void = {
assert(dict.is_empty(dict.new()))
d = dict.set(dict.new(), "a", 1)
assert(!dict.is_empty(d))
}merge
merge: (A: Type, B: Type)(a: &Dict(A, B), b: &Dict(A, B)) -> Dict(A, B)Merges two dictionaries and returns a new dictionary. Both source dictionaries are read-only borrows and remain unchanged.
On a key conflict, the value from b overrides the value from a.
Error: Throws E6007 when either argument is not a dictionary.
use std.assert
use std.dict
main: () -> Void = {
m = dict.merge(dict.set(dict.new(), "x", 10), dict.set(dict.new(), "y", 20))
assert(dict.get(m, "x") == 10)
assert(dict.get(m, "y") == 20)
}Related
std.list— Handles the return values ofkeys/values/entries- Error Code Reference —
E6008missing key
