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 NdArrayErrors 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
| Module | Use it for |
|---|---|
std.strings | Splitting, joining, replacing, searching, trimming, case conversion, and parsing numbers |
std.unicode | Counting, slicing, and changing case by Unicode characters rather than bytes |
std.regex | Matching, captures, replacement, and splitting with regular expressions |
std.fmt | Width, precision, padding, numeric bases, and reusable format strings |
std.json | JSON values, parsing, printing, and typed struct or enum codecs |
std.csv | Reading and writing CSV rows and tables |
std.template | Small {{key}} string templates |
std.bytes | Building 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 type | Use it for |
|---|---|
Array<T> | Growable ordered collections and eager map/filter/group operations |
Iterator<T> | Lazy, single-pass collection pipelines |
std.set | Unique values and set operations |
std.counter | Counting occurrences and finding common values |
std.heap | Priority queues ordered by a comparison function |
NdArray<T> | Dense numerical arrays with shapes, views, broadcasting, and matrix operations |
std.math | Trigonometry, roots, powers, logarithms, clamps, and integer helpers |
std.random | Seeded random numbers, choice, shuffle, and sampling |
std.convert | Explicit 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
| Module | Use it for |
|---|---|
std.fs | Whole-file helpers, streaming file handles, directories, metadata, and file watching |
std.path | Joining, normalizing, and inspecting paths without touching the filesystem |
std.io | Standard input and output |
std.os | Environment variables, working directory, platform information, and signals |
std.process | Program arguments, running child processes, and streaming their input or output |
std.time | Current and monotonic time, sleeping, DateTime, and ISO-8601 conversion |
std.log | Structured 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
| Module | Use it for |
|---|---|
std.http | HTTP requests, response streaming, and downloads |
std.http_server | Request handlers, servers, streaming bodies, multipart input, and file responses |
std.sql | PostgreSQL connections, safe parameters, rows, pools, transactions, and streaming |
std.orm | Typed PostgreSQL models, queries, relations, and migrations |
The focused guides show the normal workflows:
Concurrency and interoperability
| Module | Use it for |
|---|---|
std.channel | Bounded channels between async tasks |
std.isolate | A named function running independently on another core |
std.parallel | Mapping or chunking collection work across cores |
std.shared | Large immutable values shared by isolates |
std.ffi | Copying 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
| Module | Use it for |
|---|---|
std.testing | Assertions in test_* functions |
std.args | Typed command-line flags and subcommands |
std.build | Named project tasks and external build steps |
std.encoding | Base64 and hexadecimal encoding |
std.hash | SHA-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.