Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Standard Library

Verified against crush-ast v0.13.0 (dc0d0278, 2026-12-03).

Crush’s “standard library” is a set of capabilities — dotted functions such as str.split and math.sqrt — that the host registers. It is not a module system: there is no import std.io, and import does not load anything yet (crush-ast CRUSH-110). You call these functions by their full dotted name.

Turning it on

It is on by default (crush-ast CRUSH-113): the stdlib Cargo feature of crush-lang-sdk is a default feature, and crush-run/crush run register the library without a flag, because these capabilities confer no authority. They are pure computation and do no I/O; for files, network, time and processes see Capability System.

  • --stdlib is still accepted, for compatibility, and changes nothing.
  • --no-stdlib leaves the library out. A program that uses it is then refused before it runs, with stdlib (default; --no-stdlib): str.to_upper.
  • A build with default-features = false (an embedder can choose that) has no library; --stdlib is then an error.

Arguments are positional and the arity is checked ([runtime] str.replace takes 3 arg(s), got 2). Note that some predicates return 1/0 rather than true/false (for example regex.test and collections.includes), while the str.contains, str.starts_with and str.ends_with predicates return a Bool (starts_with / ends_with returned 1/0 before v0.12.0). Test a predicate’s result as a condition rather than comparing it with == 1 or == true.

Strings — str.*

FunctionResult
str.split(s, delim)array of parts
str.join(array, delim)string
str.trim(s), str.trim_start(s), str.trim_end(s)string
str.replace(s, old, new)string
str.contains(s, sub), str.starts_with(s, p), str.ends_with(s, p)Bool
str.to_upper(s), str.to_lower(s)string
str.pad_left(s, width, fill), str.pad_right(s, width, fill)string
str.repeat(s, n)string
str.substring(s, start, end)string (end exclusive)
str.char_at(s, i), str.index_of(s, sub)one-char string / index
str.format(template, value)the first {} replaced by value (exactly two arguments)
str.format(template, args){} placeholders filled from args

str.concat(...) and str.len(s) are always available, even with --no-stdlib.

print(str.split("a,b,c", ","))
print(str.join(["a", "b"], "-"))
print("[" + str.trim("  x  ") + "]")
print(str.replace("hello world", "world", "crush"))
print(str.to_upper("abc"))
print(str.pad_left("7", 3, "0"))
print(str.repeat("ab", 3))
print(str.substring("hello", 1, 3))
print(str.index_of("hello", "l"))
[a, b, c]
a-b
[x]
hello crush
ABC
007
ababab
el
2

(Arrays print as [a, b, c] — strings inside an array are shown unquoted.)

Math — math.*

math.sqrt, math.abs, math.floor, math.ceil, math.round, math.sin, math.cos, math.tan, math.pow(base, exp), math.min(a, b), math.max(a, b), math.pi(). Results are Float. math.random(), math.random_int(lo, hi) (requires lo < hi) and math.seed(n) use a deterministic built-in generator (crush-ast CRUSH-116), so seeded runs are reproducible.

print(math.sqrt(16))
print(math.pow(2, 10))
print(math.floor(-1.5))
print(math.round(3.6))
print(math.max(2.5, 1))
4.0
1024.0
-2.0
4.0
2.5

Conversion — conv.*

conv.to_int, conv.to_float, conv.to_str, conv.to_bool, conv.parse_int(text, radix), conv.parse_float, conv.type_of, plus the always-on conv.chr / conv.ord.

print(conv.to_int("42") + 1)
print(conv.to_int(3.9))
print(conv.parse_int("ff", 16))
print(conv.parse_int("101", 2))
print(conv.to_float("2"))
print(conv.type_of(3.5))
43
3
255
5
2.0
float

Collections — collections.*

collections.len, reverse, includes, flatten, chunk(array, n), zip, unique, all, any, find, sort_by(array, key), pluck(array, key), and for objects: keys, values, entries, merge.

let scores = {"ada": 3, "grace": 5}
print(collections.keys(scores))
print(collections.values(scores))
print(collections.keys(collections.merge(scores, {"linus": 1})))
print(collections.reverse([1, 2, 3]))
print(collections.chunk([1, 2, 3, 4], 2))
print(collections.unique([1, 1, 2, 2, 3]))
[ada, grace]
[3, 5]
[ada, grace, linus]
[3, 2, 1]
[[1, 2], [3, 4]]
[1, 2, 3]

Objects keep their keys in insertion order: collections.keys(obj), collections.values(obj), collections.entries(obj), for k in obj and printing all walk the keys in the order they were first set (not sorted):

let m = {"b": 2, "a": 1}
for k in collections.keys(m) {
    print(k)
}
print(collections.entries(m))
b
a
[[b, 2], [a, 1]]

JSON — json.*

json.parse(text) → object/array/scalar; json.stringify(value); json.stringify_pretty(value).

let o = json.parse("{\"name\": \"Ada\", \"n\": 3}")
print(o.name)
print(o.n + 1)
print(json.stringify({"x": 1}))
Ada
4
{"x":1}

Paths — path.*

path.join(a, b), dirname, basename, extension, stem, is_absolute, normalize — pure string manipulation, no filesystem access.

print(path.join("a", "b.txt"))
print(path.dirname("/x/y/z.txt"))
print(path.basename("/x/y/z.txt"))
print(path.stem("z.tar.gz"))
print(path.normalize("a/./b/../c"))
a/b.txt
/x/y
z.txt
z.tar
a/c

Regular expressions — regex.*

The pattern comes first, then the text: regex.test(pattern, text), regex.find_all(pattern, text), regex.replace(pattern, text, replacement), regex.split(pattern, text).

print(regex.find_all("[0-9]+", "a1b22"))
print(regex.replace("[0-9]", "a1b2", "#"))
print(regex.split("[,;] *", "a, b;c"))
[1, 22]
a#b#
[a, b, c]

regex.match(pattern, s) returns the whole match followed by the groups. (It used to be a parse error, because match is a keyword; since crush-ast CRUSH-394 a keyword is accepted as a name after a dot.)

print(regex.match("([a-z]+)([0-9]+)", "abc123"))
[abc123, abc, 123]

Other families

Also in the standard library: bytes.* (from_string, to_string, len, slice), buffer.* (alloc, read, write, freeze), binary.read_* / binary.write_* (u16/u32/u64, big/little endian), result.* (ok, err, is_ok, unwrap — results are plain {ok, value} objects), text.sort, text.uniq, time.format, time.parse, env.os, env.arch, and the system.* helpers implemented in Crush itself (crates/crush-lang-sdk/sbl/).

print(result.unwrap(result.ok(5)))
print(result.ok(5))
print(result.is_ok(result.err("no")))
print(str.format("Hello {}!", "World"))
5
{ok: true, value: 5}
false
Hello World!

env.os() and env.arch() return the host’s OS and CPU names (linux, x86_64), so their output depends on the machine.

Not in the library

There is no std.* module tree, io.eprint, array.length or map.*. For output use print/io.print; for the process environment use env.get (--env). Program arguments are sys.args(), an array of what follows the file name on the command line (crush run prog.crush a b → [a, b]), and sys.exit(code) stops the program with that exit status.

Next Steps