Skip to content

std.dict ​

The operations module for dictionaries (Dict(K, V)).

yaoxiang
use std.dict

Semantic Categories ​

CategoryFunctionBehavior
Read-only borrowget has values keys entries len is_empty mergeThe source dictionary can be reused
No argumentsnewConstruct an empty dictionary
Consumes the source dictionaryset deleteThe source dictionary is moved and cannot be used afterward
yaoxiang
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 ​

FunctionSignature
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 ​

yaoxiang
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 is Void), not an empty dictionary (src/std/dict.rs:97-100). The only way to construct an empty dictionary is dict.new(). The dictionary literal { "k": v, … } can still be used; see Syntax Specification §1.6.4.

yaoxiang
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 ​

yaoxiang
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.

yaoxiang
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 ​

yaoxiang
get: (K: Type, V: Type)(dict: &Dict(K, V), key: Any) -> Any

Retrieves 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.

yaoxiang
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:

yaoxiang
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 ​

yaoxiang
has: (K: Type, V: Type)(dict: &Dict(K, V), key: Any) -> Bool

Whether key exists in the dictionary.

Error: Throws E6007 when the first argument is not a dictionary.

yaoxiang
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 ​

yaoxiang
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.

yaoxiang
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 ​

yaoxiang
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.

yaoxiang
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 ​

yaoxiang
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.

yaoxiang
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 ​

yaoxiang
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.

yaoxiang
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 ​

yaoxiang
len: (K: Type, V: Type)(dict: &Dict(K, V)) -> Int

Number of entries. Read-only borrow; dict can be reused.

Error: Throws E6007 when the argument is not a dictionary.

yaoxiang
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 ​

yaoxiang
is_empty: (K: Type, V: Type)(dict: &Dict(K, V)) -> Bool

Whether the dictionary is empty.

Error: Throws E6007 when the argument is not a dictionary.

yaoxiang
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 ​

yaoxiang
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.

yaoxiang
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)
}