Skip to content

std/io

Import with import std/io.

Modules

Types

enum IOError {
    NotFound { path: String }
    Other { reason: String }
}

Distinguishable filesystem error variants returned by read_file and write_file. NotFound carries the path so callers can decide whether to treat absence as expected (e.g., first run of an app that creates its data file on demand). Other wraps any other failure with its raw error message.

fn print<'T>(value: 'T): Unit where 'T: Display

Prints a value’s user-facing representation to stdout, followed by a newline. The argument may be any value whose type implements Display — via an impl Display for T block or derive Display — and is stringified via Display.to_string. String implements Display as the identity, so IO.print("hello") prints hello unchanged; no need to pre-stringify other values. Use IO.inspect for the developer-facing Debug form.

IO.print("hello")   // prints: hello
IO.print(42)        // prints: 42
fn inspect<'T>(value: 'T): Unit

Prints a value’s runtime representation to stdout via Debug.inspect. Debug is universal — every value is inspectable with no derive/impl required (a structural impl is auto-synthesized for any type that doesn’t supply its own; opaque types render <opaque T>), so inspect carries no Debug bound. Write an explicit impl Debug implementation only to customize the rendering. Use dbg when you want to inspect a value while preserving it in an expression or pipeline.

IO.inspect([1, 2, 3])   // prints: [1, 2, 3]
fn read_line(): Result<String, String>

Reads a line from stdin. Returns Ok(line) with the newline stripped, or Err(“eof”) when stdin is closed.

case IO.read_line() {
  Ok(line) -> IO.print("got: " + line)
  Err(_) -> IO.print("done")
}
fn read_file(path: String): Result<String, IOError>

Reads the entire contents of path into a String.

IO.read_file("notes.txt")   // Ok("...") or Err(IO.IOError.NotFound{...})
fn write_file(path: String, content: String): Result<Unit, IOError>

Writes content to path, replacing any existing file. The parent directory must exist; missing parents do not get auto-created.

IO.write_file("notes.txt", "hello")