Skip to content

Standard Library Reference ​

The YaoXiang standard library (std) is organized into modules. Each module is imported with use and then invoked as module.function(...). This directory contains API reference documentation split by module.

Module Index ​

ModuleExportsDescription
std.convert11Conversion from arbitrary values to String
std.dict11Dictionary access, key-value views, and merging
std.fs22File, directory, and path operations
std.io4Standard output, standard input, and formatting
std.math18Integer, floating-point, and trigonometric functions, including PI/E/TAU constants
std.string21String searching, splitting, formatting, and parsing
std.time14Timestamps, formatting, and date component access
std.result8Construction and unpacking of Result and Error
std.range10Range iteration, predicates, and lazy adapters
std.assert1Assertions
std.net4HTTP requests and URL percent encoding/decoding
std.concurrent3Sleeping, yielding the scheduler, and thread identification
std.os12File handles, environment variables, and working directory
std.weak2Arc / Weak weak references

Modules Implemented in Pure YaoXiang ​

The following modules are implemented in .yx source code and embedded in the binary (src/std/yx_sources.rs). They are not native StdModules, so they are outside the generation scope of the table above (modules_for_docs() at src/std/gen_docs.rs:43 explicitly excludes them). Their pages are handwritten, and their signatures are taken verbatim from the .yx source files.

ModuleSourceDescription
std.listsrc/std/list.yxList insertion and deletion, slicing, higher-order functions, and the iterator protocol
std.jsonsrc/std/json.yxJSON parsing and serialization (RFC 8259)
std.optionsrc/std/option.yxOption(T) optional values and sum types
std.testsrc/std/test.yxTest assertion library (value semantics, RFC-036 §3)

std.result is a dual-implementation module: the native utility family (src/std/result.rs) and the pure yx Result type (src/std/result.yx) are combined into a single export surface—Result variant construction goes through yx, while utilities such as is_ok / unwrap / code go through native. See std.result.

Import Conventions ​

Importing an entire module:

yaoxiang
use std.list
use std.string

main: () -> Void = {
    parts = string.split("a,b,c", ",")
    println(list.len(parts))
}

You can also import individual names from a module, including constants:

yaoxiang
use std.assert
use std.math.{E, PI, TAU}

main: () -> Void = {
    assert(PI > 3.14)
}

Parameter Borrowing Conventions ​

A & in a signature denotes a read-only automatic borrow (RFC-009 §2.8): variables passed by the caller are not moved and remain usable after the call. This is the default form for many read-only functions in the standard library.

yaoxiang
use std.assert
use std.list

main: () -> Void = {
    nums = [1, 2, 3]

    // All three calls borrow nums read-only, so it remains usable afterward
    assert(list.len(nums) == 3)
    assert(list.len(nums) == 3)
    assert(list.contains(nums, 2))
}

A parameter without & is passed by value. Consequently, most "modifying" functions follow a functional form that consumes the source value and returns a new value, rather than modifying it in place:

yaoxiang
use std.assert
use std.list

main: () -> Void = {
    base = [1, 2]
    extended = list.push(base, 3)   // Returns a new list; base has been moved
    assert(list.len(extended) == 3)
}

The “Semantic Categories” section of each module page lists which functions in that module borrow, consume, or modify values in place. Pay particular attention to value retrieval and bounds: list.get / list.first / list.last / list.slice all use []'s bounds checks and report E6003 when out of bounds or when the list is empty; they do not return sentinel values.

Error Model ​

The standard library has two forms of failure, labeled “Error” and “Return …” in the individual function entries:

FormBehaviorTypical use cases
Throw a runtime errorTerminates the current execution with an E6xxx codeMissing dictionary key E6008, out-of-bounds index E6003, assertion failure E6005
Return a sentinel valueDoes not interrupt execution; returns Void / -1 / ""list.find_index finds no match, string.index_of finds no match, environment variable is missing

list is a pure YaoXiang implementation (src/std/list.yx): value-access functions use [] indexing directly without clamping bounds, so an out-of-bounds access immediately results in E6003 (src/std/list.yx:77-79); retrieving the first or last element of an empty list behaves the same way (src/std/list.yx:82-99).

Common runtime error codes:

Error codeMeaningTrigger example
E6003Index out of boundslist.set(l, 99, v)
E6005Assertion failureassert(false)
E6007Generic runtime errorFile does not exist, result.unwrap fails
E6008Missing keydict.get(d, "nope")
E6010Integer parsing failure (returned as an Err value)string.parse_int("abc")
E6011Floating-point parsing failure (returned as an Err value)string.parse_float("abc")

For the complete error-code table, see the Error Code Reference.

string.parse_int / string.parse_float use a third form: they do not throw an error; instead, they wrap failures in and return an Err value of Result, which can be unpacked with std.result or propagated with ?.

yaoxiang
use std.assert
use std.result
use std.string

main: () -> Void = {
    assert(result.is_ok(string.parse_int("42")))
    assert(result.is_err(string.parse_int("abc")))
}

Iteration Protocol ​

Both std.list and std.range provide iter / has_next / next, but the parameter forms on the two sides are opposite. Copying the syntax from either side to the other will result in a mismatch:

Functionstd.list (src/std/list.yx)std.range (src/std/range.rs)
has_next(it: &Iter(T)) -> Bool(it: Iterator(Any)) -> Bool
next(it: &mut Iter(T)) -> T(it: &Iterator(Any)) -> Any

In other words, on the list side, the iterator is borrowed (next takes &mut, and has_next takes &), so the same iterator can be used to retrieve elements successively. On the range side, has_next consumes the iterator by value (its signature has no &), so it must be recreated for every check; only next borrows it. For everyday iteration, use for ... in directly and you need not worry about either form.

yaoxiang
use std.assert
use std.list

main: () -> Void = {
    it = list.iter([1, 2, 3])
    assert(list.has_next(it))

    // On the list side, it is borrowed: the same iterator can retrieve elements repeatedly
    assert(list.next(it) == 1)
    assert(list.next(it) == 2)
}

For everyday iteration, use for ... in directly:

yaoxiang
use std.assert

main: () -> Void = {
    mut sum = 0
    for x in [1, 2, 3] {
        sum = sum + x
    }
    assert(sum == 6)
}

range.map / range.filter return lazy adapters, which produce results only after being consumed by collect / reduce / for_each / for ... in:

yaoxiang
use std.assert
use std.list
use std.range
use std.result

main: () -> Void = {
    doubled = range.collect(range.map(result.unwrap(range.iter(1..4)), x => x * 2))
    assert(list.get(doubled, 0) == 2)
    assert(list.len(doubled) == 3)
}

Platform Availability ​

The following depend on operating-system capabilities and are not exported for wasm32 targets:

ScopeRequires
All of std.os, all of std.net, and all of std.weakFiles/network
All of std.concurrentThreads
std.io.read_lineStandard I/O
All of std.fsFile system
std.time.sleepThread sleep

std.string / std.dict / std.math / std.convert / std.result / std.range / std.assert, the pure yx std.list / std.json / std.option / std.test, and std.io.print / println / format_fallback are available on all targets.

Known Gaps ​

The following items were each verified by actually running the examples while writing this documentation:

LocationOriginal issueCurrent status
os.openThe handle is single-use, so open→write→close cannot compileHandles are now passed by reference ✅
time.datetime_*Eight accessors cannot be called from source code because their exported names contain ::Flattened names such as datetime_year are now used ✅
time.parse_timeThe fmt parameter is ignored; the return value cannot be used furtherParsing now advances according to fmt and returns an Int ✅
math.clampmin > max panics the interpreter instead of returning an errorNow returns E6007 ✅
net.http_get / http_postIt was a placeholder implementation and did not issue requestsNow a real implementation (ureq synchronous blocking + rustls TLS, issue #56) ✅

Open issues:

LocationIssueTracking
std.optionThe instantiation of Try(Option(T), T, Void) is not accepted by the type checker; ? reports E1081; from_error reports E6006 at runtimeSee “Known Gaps” on that page
list.sliceOut-of-bounds indices are not clamped and directly result in E6003See the corresponding entry on that page

Documentation Maintenance ​

This directory uses a generated + handwritten hybrid structure:

  • Generated sections (between <!-- stdlib:KEY start/end --> markers): function overview tables and signature blocks derived from StdModule::exports(). Signatures are copied byte-for-byte from NativeExport::signature, so they cannot drift from the implementation.
  • Handwritten sections (outside the markers): module overviews, borrowing/movement semantics, the error model, known gaps, and examples.

CI gates (run alongside cargo test --lib):

GateTestPurpose
Drift detectiontest_stdlib_docs_match_generationGenerated sections must match exports()
Orphan detectiontest_stdlib_docs_has_no_orphan_module_pagesModule pages must not outnumber the generator's output
Coveragetest_stdlib_docs_covers_interface_modulesThe documented module set must cover the interface view
Examples runnabletest_stdlib_docs_examples_runEvery ```yaoxiang example must actually run

The generator covers only native StdModules; pages for pure yx modules (list / json / option / test) contain no generated sections. Their validity is jointly ensured by the runnable-examples gate and orphan detection (the page names are on the allowlist in src/std/yx_sources.rs).

After changing exports(), use the repair tool to rewrite the generated sections:

bash
cargo run --example gen-stdlib-docs

This is analogous to gen-std-interfaces (RFC-037 interface view) and tools/code-tables --fix (RFC-013 code table).