For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).
StringDict
struct StringDict[V: Movable]
Container used to pass owned variadic keyword arguments to functions.
This type mimics the interface of a dictionary with String keys, and
should be usable more-or-less like a dictionary. Notably, however, this type
should not be instantiated directly by users.
Parameters
- V (
Movable): The value type of the dictionary. Must beMovable. WhenVis notImplicitlyDeletable, the dictionary has no implicit destructor and must be torn down withdeinit_with().
Implemented traits
AnyType,
Copyable,
Defaultable,
ImplicitlyDeletable,
Iterable,
Movable,
Sized
comptime members
IteratorType
comptime IteratorType[iterable_mut: Bool, //, iterable_origin: Origin[mut=iterable_mut]] = _DictKeyIter[String, V(Copyable), Fnv1a, iterable_origin]
The iterator type for this dictionary.
Parameters
- iterable_mut (
Bool): Whether the iterable is mutable. - iterable_origin (
Origin[mut=iterable_mut]): The origin of the iterable.
key_type
comptime key_type = String
The key type for this dictionary (always String).
Methods
__init__
def __init__(out self)
Initialize an empty keyword dictionary.
__del__
def __del__(deinit self) where conforms_to(V, ImplicitlyDeletable)
Destroy all values in the dictionary and free memory.
Constraints:
V must be ImplicitlyDeletable. When it is not, the dictionary has
no implicit destructor and must be torn down with deinit_with().
__getitem__
def __getitem__(ref self, ref key: String) -> ref[self_is_mut._dict["value"]] V
Retrieve a value out of the keyword dictionary.
Args:
- key (
String): The key to retrieve.
Returns:
ref[self_is_mut._dict["value"]] V: The value associated with the key, if it's present.
Raises:
DictKeyError if the key isn't present.
__setitem__
def __setitem__(mut self, key: String, var value: V) where conforms_to(V, ImplicitlyDeletable)
Set a value in the keyword dictionary by key.
Constraints:
V must be ImplicitlyDeletable, since assigning to an existing key
destroys the displaced value in place. To populate a keyword
dictionary with a linear V, use insert() instead.
Args:
- key (
String): The key to associate with the specified value. - value (
V): The data to store in the dictionary.
__contains__
def __contains__(self, key: String) -> Bool
Check if a given key is in the keyword dictionary or not.
Args:
- key (
String): The key to check.
Returns:
Bool: True if there key exists in the keyword dictionary, False
otherwise.
deinit_with
def deinit_with(deinit self, deinit_func: T, /) where (eq T.V, V)
Consume the dictionary, deinitializing each key/value pair with a closure.
Use this to tear down a keyword dictionary whose values are not
ImplicitlyDeletable.
Args:
- deinit_func (
T): A closure called once per entry to destroy its key and value.
__len__
def __len__(self) -> Int
The number of elements currently stored in the keyword dictionary.
Returns:
Int: The number of elements currently stored in the keyword dictionary.
find
def find(self, key: String) -> Optional[V] where conforms_to(V, Copyable)
Find a value in the keyword dictionary by key.
Args:
- key (
String): The key to search for in the dictionary.
Returns:
Optional[V]: An optional value containing a copy of the value if it was present,
otherwise an empty Optional.
insert
def insert(mut self, var key: String, var value: V) -> Optional[SwissTableEntry[String, V, Fnv1a]]
Insert a key/value pair, returning the displaced entry if the key was already present.
Unlike __setitem__, the displaced entry is moved out and returned
(never destroyed in place), so this works when V is linear
(non-ImplicitlyDeletable). The caller is responsible for disposing of
the returned entry.
Args:
- key (
String): The key to associate with the value. - value (
V): The value to store.
Returns:
Optional[SwissTableEntry[String, V, Fnv1a]]: The previous entry if key was already present, otherwise an empty
Optional.
popitem
def popitem(mut self) -> SwissTableEntry[String, V, Fnv1a]
Remove and return a (key, value) pair from the dictionary.
The entry is moved out whole (nothing is destroyed in place), so this
works when V is linear. The caller is responsible for disposing of the
returned entry.
Returns:
SwissTableEntry[String, V, Fnv1a]: The removed entry.
Raises:
EmptyDictError if the dictionary is empty.
pop
def pop(mut self, key: String, var default: V) -> V where conforms_to(V, ImplicitlyDeletable)
Remove a value from the dictionary by key.
Constraints:
V must be ImplicitlyDeletable, since the unused default is
discarded in place when the key is found. To remove from a keyword
dictionary with a linear V, use pop(key) or popitem().
Args:
- key (
String): The key to remove from the dictionary. - default (
V): A default value to return if the key was not found instead of raising.
Returns:
V: The value associated with the key, if it was in the dictionary.
If it wasn't, return the provided default value instead.
def pop(mut self, ref key: String) -> V
Remove a value from the dictionary by key.
Args:
- key (
String): The key to remove from the dictionary.
Returns:
V: The value associated with the key, if it was in the dictionary.
Raises otherwise.
Raises:
DictKeyError if the key was not present in the dictionary.
__iter__
def __iter__(ref self) -> _DictKeyIter[String, V(Copyable), Fnv1a, origin_of(self)]
Iterate over the keyword dict's keys as immutable references.
Returns:
_DictKeyIter[String, V(Copyable), Fnv1a, origin_of(self)]: An iterator of immutable references to the dictionary keys.
keys
def keys(ref self) -> _DictKeyIter[String, V(Copyable), Fnv1a, origin_of(self._dict)]
Iterate over the keyword dict's keys as immutable references.
Returns:
_DictKeyIter[String, V(Copyable), Fnv1a, origin_of(self._dict)]: An iterator of immutable references to the dictionary keys.
values
def values(ref self) -> _DictValueIter[String, V(Copyable), Fnv1a, origin_of(self._dict)] where conforms_to(V, Copyable)
Iterate over the keyword dict's values as references.
Returns:
_DictValueIter[String, V(Copyable), Fnv1a, origin_of(self._dict)]: An iterator of references to the dictionary values.
items
def items(ref self) -> _DictEntryIter[String, V(Copyable), Fnv1a, origin_of(self._dict)] where conforms_to(V, Copyable)
Iterate over the keyword dictionary's entries as immutable references.
Examples:
var my_dict = Dict[String, Int]()
my_dict["a"] = 1
my_dict["b"] = 2
for e in my_dict.items():
print(e.key, e.value)
Notes: These can't yet be unpacked like Python dict items, but you can access the key and value as attributes.
Returns:
_DictEntryIter[String, V(Copyable), Fnv1a, origin_of(self._dict)]: An iterator of immutable references to the dictionary entries.