Skip to content

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.

yaoxiang
use std.os

All functions in this module depend on operating system capabilities and are not exported on the wasm32 target.

File Handle Model ​

Handles are passed by reference (#337 has been fixed): the signatures of read / write / seek / tell / flush / close are all (file: &File, ...), so a handle can be reused:

yaoxiang
f = os.open(p, "w")
os.write(f, "hello world")
os.close(f)

Positioned reads and writes (the reason seek exists) are also available:

yaoxiang
r = 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, and open → write → close would report E2014.

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:

yaoxiang
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:

ModeMeaning
rRead-only; file must exist
wWrite-only; create or truncate
aAppend; 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 ​

FunctionSignature
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 ​

yaoxiang
open: (path: &String, mode: &String) -> File

Opens 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.

yaoxiang
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 ​

yaoxiang
close: (file: &File) -> Void

Closes 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).

yaoxiang
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 ​

yaoxiang
read: (file: &File, n: Int) -> String

Reads at most n bytes from the current read/write position.

  • file — file handle
  • n — 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.

yaoxiang
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 ​

yaoxiang
write: (file: &File, content: String) -> Int

Writes 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.

yaoxiang
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 ​

yaoxiang
seek: (file: &File, offset: Int) -> Bool

Moves 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.

yaoxiang
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 ​

yaoxiang
tell: (file: &File) -> Int

Returns the byte offset of the current read/write position.

Error: throws E6007 if the handle is invalid.

yaoxiang
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 ​

yaoxiang
flush: (file: &File) -> Void

Flushes buffered content to disk.

Error: throws E6007 if the handle is invalid or the flush fails.

yaoxiang
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 ​

yaoxiang
get_env: (name: &String) -> String

Reads 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.”

yaoxiang
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 ​

yaoxiang
set_env: (name: &String, value: &String) -> Void

Sets an environment variable (affecting the current process).

yaoxiang
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 ​

yaoxiang
args: () -> String

Returns 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.

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

main: () -> Void = {
    argv = os.args()
    assert(string.len(argv) > 0)
}

chdir ​

yaoxiang
chdir: (path: &String) -> Bool

Changes the current working directory.

Returns: true on success. Error: throws E6007 if the directory does not exist.

yaoxiang
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 ​

yaoxiang
getcwd: () -> String

Returns the absolute path of the current working directory.

Error: throws E6007 if it cannot be retrieved.

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

main: () -> Void = {
    cwd = os.getcwd()
    assert(string.len(cwd) > 0)
}