std.os
Operating system interface module: file handle reads and writes, environment variables, and working directories. For path-level file operations (whole-file reads and writes, directories, metadata, and path manipulation), see std.fs.
use std.osAll functions in this module depend on operating system capabilities and are not exported on the
wasm32target.
File Handle Model
Handles are passed by reference (#337 has been fixed): the signatures of
read/write/seek/tell/flush/closeare all(file: &File, ...), so a handle can be reused:yaoxiangf = os.open(p, "w") os.write(f, "hello world") os.close(f)Positioned reads and writes (the reason
seekexists) are also available:yaoxiangr = os.open(p, "r") os.seek(r, 6) tail = os.read(r, 5) // "world" os.close(r)Before the fix, the signatures had no
&, so handles were passed by value → linear ownership → they became invalid after one use, andopen → write → closewould reportE2014.
To avoid manually managing handles, you can use the convenience functions that do not open handles—they are in std.fs (not in std.io): fs.read_file / fs.write_file / fs.append_file. This module (std.os) only provides handle-level incremental reads and writes and has no append_file (os.append_file reports E1042).
The return type annotation of open is File—the engine internally maintains a handle table (src/std/os.rs:98). The underlying type is std::fs::File, which is an opaque handle value to users.
Content is written to disk immediately after writing, with no need to explicitly call close:
use std.assert
use std.fs
use std.os
main: () -> Void = {
p = "__yx_doc_open.txt"
n = os.write(os.open(p, "w"), "hello")
assert(n == 5)
assert(fs.read_file(p) == "hello")
fs.remove(p)
}The modes supported by open are:
| Mode | Meaning |
|---|---|
r | Read-only; file must exist |
w | Write-only; create or truncate |
a | Append; create or append to the end |
r+ | Read/write; file must exist |
w+ | Read/write; create or truncate |
a+ | Read/write; create or append |
Function Overview
| Function | Signature |
|---|---|
open | (path: &String, mode: &String) -> File |
close | (file: &File) -> Void |
read | (file: &File, n: Int) -> String |
write | (file: &File, content: String) -> Int |
seek | (file: &File, offset: Int) -> Bool |
tell | (file: &File) -> Int |
flush | (file: &File) -> Void |
get_env | (name: &String) -> String |
set_env | (name: &String, value: &String) -> Void |
args | () -> String |
chdir | (path: &String) -> Bool |
getcwd | () -> String |
File Operations
open
open: (path: &String, mode: &String) -> FileOpens a file and returns a handle.
path— file path (read-only borrow)mode— open mode; see the table above
Returns: a File handle value. Handles are passed by reference—the parameters of write / seek / read / tell / flush / close are all &File (src/std/os.rs:35-65), so the same handle can be reused until it is explicitly closeed. Usually, open is inlined into a single call to avoid cleanup.
Error: throws E6007 if the mode is invalid, the file does not exist, or permission is denied.
use std.assert
use std.fs
use std.os
main: () -> Void = {
p = "__yx_doc_open_only.txt"
f = os.open(p, "w")
assert(fs.exists(p))
os.close(f)
fs.remove(p)
}close
close: (file: &File) -> VoidCloses the file handle and releases its table entry.
The parameter is &File, so close can be placed at the end of a sequence of reads and writes to release the handle after it has been used. Written content has already reached the disk when write returns (os.open(…, "w") uses OpenOptions::create(true)), so explicit closing is usually unnecessary—however, handles should be released when holding many of them for an extended period.
Error: throws E6007 if the handle is invalid (never opened or already closed).
use std.fs
use std.os
main: () -> Void = {
p = "__yx_doc_close.txt"
f = os.open(p, "w")
os.write(f, "data")
os.close(f)
fs.remove(p)
}read
read: (file: &File, n: Int) -> StringReads at most n bytes from the current read/write position.
file— file handlen— number of bytes expected to be read
Returns: the content actually read (which may be shorter than n and is an empty string at the end of the file). Invalid UTF-8 bytes are returned as replacement characters without raising an error. Error: throws E6007 if the handle is invalid or the read fails.
use std.assert
use std.fs
use std.os
main: () -> Void = {
p = "__yx_doc_read.txt"
fs.write_file(p, "abcdef")
part = os.read(os.open(p, "r"), 3)
assert(part == "abc")
fs.remove(p)
}write
write: (file: &File, content: String) -> IntWrites all content at the current read/write position.
content— passed by value
Returns: the number of bytes written. Error: throws E6007 if the handle is invalid or the write fails.
use std.assert
use std.fs
use std.os
main: () -> Void = {
p = "__yx_doc_write.txt"
n = os.write(os.open(p, "w"), "hello")
assert(n == 5)
fs.remove(p)
}seek
seek: (file: &File, offset: Int) -> BoolMoves the read/write position to the absolute offset offset (relative to the beginning of the file).
offset— target byte offset; must be non-negative
Returns: true on success. Error: throws E6007 if the handle is invalid or the offset is invalid.
use std.assert
use std.fs
use std.os
main: () -> Void = {
p = "__yx_doc_seek.txt"
fs.write_file(p, "abcdef")
ok = os.seek(os.open(p, "r"), 2)
assert(ok)
fs.remove(p)
}tell
tell: (file: &File) -> IntReturns the byte offset of the current read/write position.
Error: throws E6007 if the handle is invalid.
use std.assert
use std.fs
use std.os
main: () -> Void = {
p = "__yx_doc_tell.txt"
pos = os.tell(os.open(p, "w"))
assert(pos == 0)
fs.remove(p)
}flush
flush: (file: &File) -> VoidFlushes buffered content to disk.
Error: throws E6007 if the handle is invalid or the flush fails.
use std.assert
use std.fs
use std.os
main: () -> Void = {
p = "__yx_doc_flush.txt"
os.flush(os.open(p, "w"))
assert(fs.exists(p))
fs.remove(p)
}Environment Variables
get_env
get_env: (name: &String) -> StringReads an environment variable.
Returns: the variable value; returns an empty string if the variable does not exist (without raising an error). Therefore, it is impossible to distinguish between “not set” and “set to an empty string.”
use std.assert
use std.os
use std.string
main: () -> Void = {
// PATH always exists on mainstream platforms
path = os.get_env("PATH")
assert(string.len(path) > 0)
// A nonexistent variable returns an empty string
assert(string.is_empty(os.get_env("__YX_DEFINITELY_MISSING__")))
}set_env
set_env: (name: &String, value: &String) -> VoidSets an environment variable (affecting the current process).
use std.assert
use std.os
main: () -> Void = {
os.set_env("__YX_DOC_ENV", "hello")
assert(os.get_env("__YX_DOC_ENV") == "hello")
}Process and Working Directory
args
args: () -> StringReturns the command-line arguments.
Returns: a single string containing all argv values joined with \n (not a List). The first item is the path to the program itself.
use std.assert
use std.os
use std.string
main: () -> Void = {
argv = os.args()
assert(string.len(argv) > 0)
}chdir
chdir: (path: &String) -> BoolChanges the current working directory.
Returns: true on success. Error: throws E6007 if the directory does not exist.
use std.assert
use std.os
main: () -> Void = {
before = os.getcwd()
assert(os.chdir(".."))
assert(os.chdir(before)) // Switch back
assert(os.getcwd() == before)
}getcwd
getcwd: () -> StringReturns the absolute path of the current working directory.
Error: throws E6007 if it cannot be retrieved.
use std.assert
use std.os
use std.string
main: () -> Void = {
cwd = os.getcwd()
assert(string.len(cwd) > 0)
}Related
std.fs— Path-level file and directory operations- Error code reference —
E6007generic runtime error
