Skip to content

Hash functions

keys

Converts the hash to a list of keys stored in the hash.

<% for fruit in fruits.keys %>
    <%= fruit %>
<% end %>
context!(
    "fruits" => HashMap::from([
        ("apples", "red"),
        ("bananas", "yellow")
    ])
);
apples
bananas

values

Converts the hash to a list of values stored in the hash.

<% for color in fruits.values %>
    <%= color %>
<% end %>
context!(
    "fruits" => HashMap::from([
        ("apples", "red"),
        ("bananas", "yellow")
    ])
);
red
yellow

iter

Converts the hash to a list of key & value tuples that are stored in the hash. When used inside a for loop, calling iter is optional.

<% for tuple in fruits %>
    <%= tuple.0 %> are <%= tuple.1 %>
<% end %>
context!(
    "fruits" => HashMap::from([
        ("apples", "red"),
        ("bananas", "yellow")
    ])
);
apples are red
bananas are yellow

empty

Returns true if the hash is empty (length 0). blank and is_empty are aliases for empty.

<%= fruits.empty %>
context!(
    "fruits" => HashMap::new(),
);
true

len

Returns the length of the hash, i.e. the number of elements stored in the hash.

<%= fruits.len %>
context!(
    "fruits" => HashMap::new(),
);
0