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

Writing a UTF-8 file

write_text(path, text) creates or replaces a file and returns the number of Unicode characters written. It writes exact UTF-8 bytes without translating newlines and closes the handle before returning. OS failures can leave truncated or partially written contents; this is not an atomic save.

Run this in a scratch directory: it replaces plenty-example.txt.

def save_and_read() -> Result[str, IoError]:
    write_text("plenty-example.txt", "Hello, é!\n")?
    read_text("plenty-example.txt")

def main() -> Result[(), AllocError | IoError]:
    print(str.repr(save_and_read())?)?
    Ok(())
Result[str, IoError].Ok("Hello, é!\n")