Skip to content

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.bytes

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.

import std.bytes
import 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 incrementally
buf := 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 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)
}
  • Immutable: b[i] = x is an error. Use bytes.builder / push_byte / finish to mutate, then re-bind.
  • push_byte wraps mod 256. push_byte(b, 300) stores 44; negatives wrap too.
  • finish range-checks. Values outside 0–255 yield .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.
  • bytes prints as [104, 105] — not as text. Use bytes.to_str for text interpretation after validating UTF-8.
  • Deep ==: b1 == b2 compares byte-by-byte on both VM and AOT.
  • Slices are zero-copy (share the store).
  • bytes.builder + push_byte + finish is O(n); never concatenate binary in a loop.
  • read_bytes caches content in the process-wide FS cache (see std.fs).
  • Immutable — safe to share across tasks without locking.
import std.bytes
import std.crypto
import std.encoding
// Hash a binary file
match 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 binary
match encoding.base64_decode_bytes("aGk=") {
.ok(raw) => println(bytes.to_ints(raw)[0]), // first byte as int
.err(e) => println(e),
}
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.

  • find/rfind/contains are O(1) setup + O(n) worst-case scan (Rust str::find).
  • str.builder + str.finish is O(n); s = s + part in a loop is O(n²).
  • str.bytes makes one copy; prefer bytes.to_ints if you already have a bytes buffer.
  • All functions are readonly/pure — safe to call concurrently from multiple tasks.

9 entries. Signatures come from the compiler’s own FuncSig table, so they match the runtime exactly; descriptions come from the module guide.

Function Signature Description
std.bytes.builder builder() -> [int] Empty byte builder handle
std.bytes.extend extend(b: [int], vs: [int]) -> [int] Append many ints
std.bytes.finish finish(vs: [int]) -> Result<bytes, str> Build bytes from ints (range-checked)
std.bytes.from_ints from_ints(vs: [int]) -> [int] Normalize int array
std.bytes.len len(v: bytes) -> int Buffer length
std.bytes.len_of len_of(vs: [int]) -> int Total size without joining
std.bytes.push_byte push_byte(b: [int], v: int) -> [int] Append byte (mod 256, negatives wrap)
std.bytes.to_ints to_ints(b: bytes) -> [int] Opaque buffer as plain ints
std.bytes.to_str to_str(vs: [int]) -> Result<str, str> Strict UTF-8 decode from ints
  • 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