Skip to content

std.io ​

I/O module. Provides standard output, standard input reading, and convenience functions for reading/writing an entire file at once. For incremental file I/O by handle, use std.os.

yaoxiang
use std.io

Platform Availability ​

Functions from read_line onward depend on OS I/O and are not exported on the wasm32 target: read_line, read_file, write_file, append_file. print / println / format_fallback are available on all targets.

Function Overview ​

FunctionSignature
print(...args) -> Void
println(...args) -> ()
read_line() -> String
read_file(path: &String) -> String
write_file(path: &String, content: &String) -> Bool
append_file(path: &String, content: &String) -> Bool
format_fallback(value, type_name: &String) -> String

Functions ​

print ​

yaoxiang
print: (...args) -> Void

Outputs all arguments in order, without a trailing newline. Multiple arguments are separated by a single space.

Arguments are formatted: String outputs its content directly; List / Dict / Tuple are expanded recursively; other values are output as literals.

yaoxiang
use std.io

main: () -> Void = {
    print("hello")
    print(" ")
    print("world")
    println("")
}

println ​

yaoxiang
println: (...args) -> ()

Same as print, but appends a newline at the end of the output.

println() without arguments outputs an empty line:

yaoxiang
main: () -> Void = {
    println("Hello, YaoXiang!")
}

read_line ​

yaoxiang
read_line: () -> String

Reads one line from standard input.

Returns: the entire line content read, with the trailing newline removed (\n or \r\n). Errors: throws E6007 on read failure.

Interactive examples cannot be run automatically in the documentation; the following is for reference only.

yaoxiang
use std.io

main: () -> Void = {
    println("Please enter your name: ")
    name = io.read_line()
    println("Hello, " + name)
}

read_file ​

yaoxiang
read_file: (path: &String) -> String

Reads the entire file content as a string at once.

  • path —— file path (immutable borrow)

Returns: the entire file content. Errors: throws E6007 when the file does not exist or permission is denied. Does not return an empty string.

yaoxiang
use std.assert
use std.io
use std.os
use std.string

main: () -> Void = {
    p = "__yx_doc_read_file.txt"
    io.write_file(p, "hello")

    content = io.read_file(p)
    assert(content == "hello")

    os.remove(p)
}

write_file ​

yaoxiang
write_file: (path: &String, content: &String) -> Bool

Writes content to path, overwriting the original content; creates the file if it does not exist.

  • path —— file path (immutable borrow)
  • content —— content to write (immutable borrow)

Returns: true on successful write. Errors: throws E6007 when the directory does not exist or permission is denied (does not return false).

yaoxiang
use std.assert
use std.io
use std.os

main: () -> Void = {
    p = "__yx_doc_write_file.txt"
    ok = io.write_file(p, "hello")
    assert(ok)
    assert(os.exists(p))
    os.remove(p)
}

append_file ​

yaoxiang
append_file: (path: &String, content: &String) -> Bool

Appends content to the end of path; creates the file if it does not exist.

Returns: true on successful write. Errors: throws E6007 on permission error.

yaoxiang
use std.assert
use std.io
use std.os

main: () -> Void = {
    p = "__yx_doc_append_file.txt"
    io.write_file(p, "hello")
    io.append_file(p, " world")
    assert(io.read_file(p) == "hello world")
    os.remove(p)
}

format_fallback ​

yaoxiang
format_fallback: (value, type_name: &String) -> String

Formats a value by type name, producing a prefixed representation such as int(42) / list@3.

This is an internal helper function used by the runtime's generic formatting fallback path; everyday code should use std.convert.to_string directly.

  • value —— any value
  • type_name —— type name string

Returns: a string representation with a type prefix.

yaoxiang
use std.assert
use std.io
use std.string

main: () -> Void = {
    s = io.format_fallback(42, "int")
    assert(string.contains(s, "42"))
}
  • std.os —— file handles, directories, and environment variables
  • std.convert —— value to string conversion