std.bytes - ZZ
Contiguous, immutable byte buffers (~1 byte RSS per byte; slices share the store with zero copies). Prints like an int array ([104, 105]). JSON-serializes as int arrays. Deep == equality. The binary-safe counterpart to std.str.
import std.bytesPurpose
Section titled “Purpose”std.bytes holds arbitrary binary data safely — no UTF-8 validation, no lossy decoding. Produced by std.fs.read_bytes, fs.read_chunk_bytes, fs.read_bytes_at, and bytes.finish. Use for file uploads, ciphertext, binary protocols, and any payload that isn’t valid UTF-8.
bytes is immutable (b[i] = x is an error). Slices (b[1:4]) share the underlying store with zero copies.
Quickstart
Section titled “Quickstart”import std.bytesimport std.fs
match fs.read_bytes("data.bin") { .ok(b) => { println(len(b)) // or b.len() println(b[0]) // u8 as int (negatives wrap) println(b[1:4]) // zero-copy slice -> bytes println(typeof(b)) // bytes for x in b { println(x) } // iterate ints } .err(e) => println(e),}
// Build binary incrementallybuf := bytes.builder()buf = bytes.push_byte(buf, 0x48) // 'H'buf = bytes.push_byte(buf, 0x65) // 'e'match bytes.finish(buf) { .ok(b) => println(b), // "He" .err(e) => println(e),}Method syntax
Section titled “Method syntax”| Method | Signature | Description |
|---|---|---|
b.len() |
bytes.len(b: bytes) -> int |
Buffer length |
b.to_ints() |
bytes.to_ints(b: bytes) -> [int] |
Opaque buffer as plain ints |
import std.bytes
func main() -> Result<int, str> { b := bytes.finish([72, 101, 108, 108, 111])? // "Hello" println(b.len()) // 5 println(bytes.to_ints(b)) // [72, 101, 108, 108, 111] return .ok(0)}Edge cases & gotchas
Section titled “Edge cases & gotchas”- Immutable:
b[i] = xis an error. Usebytes.builder/push_byte/finishto mutate, then re-bind. push_bytewraps mod 256.push_byte(b, 300)stores44; negatives wrap too.finishrange-checks. Values outside0–255yield.err.- Slices share storage.
b[1:4]is zero-copy — mutating the original through a builder invalidates nothing (immutable), but be aware slices keep the whole buffer alive. bytesprints as[104, 105]— not as text. Usebytes.to_strfor text interpretation after validating UTF-8.- Deep
==:b1 == b2compares byte-by-byte on both VM and AOT.
Performance notes
Section titled “Performance notes”- Slices are zero-copy (share the store).
bytes.builder+push_byte+finishis O(n); never concatenate binary in a loop.read_bytescaches content in the process-wide FS cache (seestd.fs).- Immutable — safe to share across tasks without locking.
Production patterns
Section titled “Production patterns”import std.bytesimport std.cryptoimport std.encoding
// Hash a binary filematch fs.read_bytes("payload.bin") { .ok(b) => { println(crypto.sha256_bytes(b)) // hex digest over raw bytes } .err(e) => println(e),}
// Base64 round-trip preserving binarymatch encoding.base64_decode_bytes("aGk=") { .ok(raw) => println(bytes.to_ints(raw)[0]), // first byte as int .err(e) => println(e),}Error model
Section titled “Error model”| Situation | Behavior |
|---|---|
find / rfind / count_in miss |
Returns -1 |
find_in / rfind_in with negative start |
Clamped to 0 |
starts_with_at / ends_with_at empty sub |
Never matches (returns false) |
ord("") or ord("ab") (not one char) |
Runtime error |
chr(negative), chr > 0x10FFFF, or surrogate |
Runtime error |
bytes.to_str(invalid_utf8) |
.err(message) — never panics |
All lookups are total (no .Result wrapper) except bytes.to_str, which is fallible because UTF-8 validation can fail.
Performance notes
Section titled “Performance notes”find/rfind/containsare O(1) setup + O(n) worst-case scan (Ruststr::find).str.builder+str.finishis O(n);s = s + partin a loop is O(n²).str.bytesmakes one copy; preferbytes.to_intsif you already have abytesbuffer.- All functions are readonly/pure — safe to call concurrently from multiple tasks.
API reference
Section titled “API reference”9 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.
See also
Section titled “See also”Built-ins— Everything that needs no import: print, len, range, map, int, str and the assert helpers.std.vec— Array pipelines: push, pop, sort, join, fold and the numeric aggregates.
Full index: API reference · Standard library