Skip to content

std.io

Standard-output and standard-error byte and string writers.

std.io exposes thin, checked writers over stdout and stderr. Each has both a bool-returning form (the legacy shape) and a Result-returning form for programs that want to propagate I/O errors with try.

import std.io as io
export def stdout_write_bytes(buf: view [u8]) -> bool
export def stderr_write_bytes(buf: view [u8]) -> bool
export def stdout_write_str(text: view str) -> bool
export def stderr_write_str(text: view str) -> bool

The bool form returns false on partial write or syscall failure; it does not surface the underlying errno.

export enum IoError:
case WriteFailed
export def stdout_write_bytes_result(buf: view [u8]) -> Result[None, IoError]
export def stderr_write_bytes_result(buf: view [u8]) -> Result[None, IoError]
export def stdout_write_str_result(text: view str) -> Result[None, IoError]
export def stderr_write_str_result(text: view str) -> Result[None, IoError]

IoError is intentionally coarse for now: it collapses every failure mode into WriteFailed. As more detail becomes useful (broken pipes, would-block), additional variants will be added.

export def stdout_write_all(buf: view [u8]) -> Result[None, IoError]
export def stderr_write_all(buf: view [u8]) -> Result[None, IoError]

The *_bytes_result calls report a short write as WriteFailed. The *_write_all variants instead loop, re-issuing the write from the first unwritten byte until every byte lands or a write genuinely fails, the right default for pipes and sockets, where a single write(2) may be partial. The single-call view str writers report failure when the syscall writes fewer bytes than the string length.

import std.io as io
def main() -> None:
io.stdout_write_str("Hello, World!\n")

For a Result-shaped variant:

import std.io as io
import std.result
def main() -> i32:
let write_result: Result[None, IoError] = io.stdout_write_str_result("Hello\n")
match write_result:
case Ok:
return 0
case Err:
return 1