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

Split text into fields

Use text.split(separator) to split on an explicit, nonempty string. It returns Result[list[str], AllocError]:

def main() -> Result[(), AllocError | IoError]:
    line = "Ada::Bea::::"
    parts = line.split("::")?
    print(parts)?
    print(str.repr(" / ".join(parts)?)?)?
    print(line)?
    print("".split(",")?)?
    Ok(())
["Ada", "Bea", "", ""]
"Ada / Bea /  / "
Ada::Bea::::
[""]

Adjacent and trailing separators preserve empty fields. Matches do not overlap: "aaaaa".split("aa") succeeds with ["", "", "a"]. A separator that does not occur produces a single piece containing the whole input.

Each piece owns independent storage. Splitting borrows both inputs and leaves them unchanged, including on allocation failure.

This program cannot produce its fields if splitting fails, so ? returns the error from main. The same policy handles failures from joining, formatting, and printing. Use a match instead when you have a useful recovery action.

An empty separator causes a runtime trap, not AllocError. Check len(separator) > 0 for user-supplied separators. Whitespace splitting without a separator and a maximum-split argument are unsupported.