Standard library guide

A guided map of the modules available under std.

moon's standard library lives under std.. Call a function by its full name, or import the names you use often:

use std.strings.{split, to_upper}
use std.math.*

fn main() {
  println(to_upper("hello"))
  println(math.sqrt(16.0))
}

Many helpers also work as methods. For example, std.strings.split(text, ",") can be written as text.split(",").

Use the online API reference for complete signatures, or read them from the terminal:

moon doc std strings
moon doc builtin Array
moon doc builtin Iterator
moon doc builtin NdArray

Errors you can handle

Functions that touch files, networks, processes, or user input often provide a try_ form that returns Result:

let text = std.fs.try_read_file("config.txt")?
let port = std.strings.try_parse_int("8080")?

Use these forms when the caller needs to distinguish one failure from another. Convenience forms are useful when a simple fallback such as "", false, or none is enough.

Text and data

ModuleUse it for
std.stringsSplitting, joining, replacing, searching, trimming, case conversion, and parsing numbers
std.unicodeCounting, slicing, and changing case by Unicode characters rather than bytes
std.regexMatching, captures, replacement, and splitting with regular expressions
std.fmtWidth, precision, padding, numeric bases, and reusable format strings
std.jsonJSON values, parsing, printing, and typed struct or enum codecs
std.csvReading and writing CSV rows and tables
std.templateSmall {{key}} string templates
std.bytesBuilding or inspecting byte sequences incrementally
use std.json

fn main() {
  let document = json.obj([
    ("name", Json.Str("moon")),
    ("stable", Json.Bool(true)),
  ])
  println(json.to_pretty(document))
}

moon strings contain UTF-8 bytes. Use std.strings when byte positions are appropriate and std.unicode when users expect character-oriented behavior.

Collections and numbers

Module or typeUse it for
Array<T>Growable ordered collections and eager map/filter/group operations
Iterator<T>Lazy, single-pass collection pipelines
std.setUnique values and set operations
std.counterCounting occurrences and finding common values
std.heapPriority queues ordered by a comparison function
NdArray<T>Dense numerical arrays with shapes, views, broadcasting, and matrix operations
std.mathTrigonometry, roots, powers, logarithms, clamps, and integer helpers
std.randomSeeded random numbers, choice, shuffle, and sampling
std.convertExplicit conversions between primitive values

Array and iterator methods include map, filter, flat_map, group_by, chunked, windowed, sorting, folds, and safe lookup methods ending in _or_none. Use an array when you need to keep or modify the result; use an iterator when values can be processed as they arrive.

See Numerical computing for NdArray<T>.

Files, paths, and processes

ModuleUse it for
std.fsWhole-file helpers, streaming file handles, directories, metadata, and file watching
std.pathJoining, normalizing, and inspecting paths without touching the filesystem
std.ioStandard input and output
std.osEnvironment variables, working directory, platform information, and signals
std.processProgram arguments, running child processes, and streaming their input or output
std.timeCurrent and monotonic time, sleeping, DateTime, and ISO-8601 conversion
std.logStructured application logging

Use whole-file helpers for small inputs:

let config = std.fs.try_read_file("config.json")?

Use a File when the input is large or needs random access:

let file = std.fs.open("video.mp4", "r")
let header = file.read_at(0, 4096)
file.close()

Start a child without involving a shell by passing an argument array:

let result = std.process.run(["git", "status", "--short"])
println(result.stdout)

Networking and databases

ModuleUse it for
std.httpHTTP requests, response streaming, and downloads
std.http_serverRequest handlers, servers, streaming bodies, multipart input, and file responses
std.sqlPostgreSQL connections, safe parameters, rows, pools, transactions, and streaming
std.ormTyped PostgreSQL models, queries, relations, and migrations

The focused guides show the normal workflows:

Concurrency and interoperability

ModuleUse it for
std.channelBounded channels between async tasks
std.isolateA named function running independently on another core
std.parallelMapping or chunking collection work across cores
std.sharedLarge immutable values shared by isolates
std.ffiCopying bytes to and from C memory and managing compatible C resources

See Concurrency and Calling C for examples and selection advice.

Testing and project support

ModuleUse it for
std.testingAssertions in test_* functions
std.argsTyped command-line flags and subcommands
std.buildNamed project tasks and external build steps
std.encodingBase64 and hexadecimal encoding
std.hashSHA-256, CRC32, FNV-1a, and other hashes

Start with Tooling for test discovery, project tasks, and editor setup. When you know the module or type name, the API reference is the shortest route to every available function.