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

Collect results into owned collections

Return a comprehension’s Result directly when the caller should handle allocation failure:

def squares() -> Result[list[i64], AllocError | ZeroDivisionError | OverflowError[i64]]:
    [n * n for n in range(6) if n % 2 == 0]

def main() -> Result[(), AllocError | IoError | AllocError | OverflowError[i64] | ZeroDivisionError]:
    print(squares()?)?
    Ok(())
[0, 4, 16]

Collect an owned iterator with recoverable list growth:

def numbers() -> Generator[i64]:
    yield 3
    yield 6

def collect() -> Result[list[i64], AllocError]:
    source = numbers()
    list[i64].from(source)

def main() -> Result[(), AllocError | IoError]:
    print(collect()?)?
    Ok(())
[3, 6]

from consumes the source. If allocation fails, it drops the partial list and remaining iterator. Neither from nor comprehensions undo earlier effects or handle failures inside the producer’s body.

Sets provide the same constructor, removing duplicates in first-seen order:

def main() -> Result[(), AllocError | IoError]:
    print(set[i64].from([3, 1, 3, 2]?)?)?
    Ok(())
{3, 1, 2}

Dictionary sources iterate over keys. from does not support borrowed sources or string iteration.