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.
use std.ioPlatform 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
| Function | Signature |
|---|---|
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
print: (...args) -> VoidOutputs 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.
use std.io
main: () -> Void = {
print("hello")
print(" ")
print("world")
println("")
}println
println: (...args) -> ()Same as print, but appends a newline at the end of the output.
println() without arguments outputs an empty line:
main: () -> Void = {
println("Hello, YaoXiang!")
}read_line
read_line: () -> StringReads 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.
use std.io
main: () -> Void = {
println("Please enter your name: ")
name = io.read_line()
println("Hello, " + name)
}read_file
read_file: (path: &String) -> StringReads 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.
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
write_file: (path: &String, content: &String) -> BoolWrites 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).
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
append_file: (path: &String, content: &String) -> BoolAppends 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.
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
format_fallback: (value, type_name: &String) -> StringFormats 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 valuetype_name—— type name string
Returns: a string representation with a type prefix.
use std.assert
use std.io
use std.string
main: () -> Void = {
s = io.format_fallback(42, "int")
assert(string.contains(s, "42"))
}Related
std.os—— file handles, directories, and environment variablesstd.convert—— value to string conversion
