Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Look up a value that may be absent

Missing keys in scores[key] are runtime errors. Use scores.get(key) when absence is expected. It returns an Option: Some(value) if found, or Nothing if absent. Match the two possibilities explicitly:

def main() -> Result[(), AllocError | IoError]:
    scores = {"Ada": 12, "Grace": 0}?
    match scores.get("Ada"):
        case Some(score):
            print(score)?
        case Nothing:
            print("unknown player")?
    print(scores.get("Grace"))?
    print(scores.get("Lin"))?
    print(scores["Ada"])?
    Ok(())
12
Option[i64].Some(0)
Option[i64].Nothing
12

A stored zero, False, empty string, or Nothing counts as present; looking up a stored Nothing returns Some(Nothing). Use the option match to choose a fallback; get has no default argument.

get leaves the dictionary and key available and does not allocate. It supports numbers, booleans, strings, and enums of such values. It cannot return mutable collections or classes: use pop to take ownership or &dictionary[key] to borrow an existing value. Dictionaries have no optional borrowed lookup yet; your own functions can return a borrow that may be absent.

Read a list element that might be missing

items.get(index) returns Some(value) for an existing element or Nothing for an out-of-range index. It takes one i64 index, including negative indices:

def main() -> Result[(), AllocError | IoError]:
    names = ["Ada", "Bea"]?
    print(names.get(-1))?
    match names.get(2):
        case Some(name):
            print(name)?
        case Nothing:
            print("no name at that position")?
    print(names)?
    Ok(())
Option[str].Some("Bea")
no name at that position
["Ada", "Bea"]

List get has the same element restrictions as dictionary get. For owned elements, use pop to transfer ownership, copy(items[index])? to duplicate one, or &items[index] to borrow it. Ordinary indexing still traps when the index is out of range. The report gives the source position, the index, and the length: error: main.plenty:3:11: index out of bounds: index 5, length 3.

Both forms of get leave their source unchanged. Numbers, booleans, and enums copy; strings share immutable storage. The returned value remains valid if the source is updated or dropped.