stdlib e embed de arquivos
Nesta página
O subconjunto comptime da stdlib cobre os casos mais comuns: métodos de string
(trim, to_upper, to_lower, concatenação) e funções de math.* (sqrt,
pow, floor, etc.). Use-os da mesma forma que usaria em tempo de execução —
a diferença é que tudo acontece no compilador.
Banner normalizado com trim + to_upper e raiz quadrada de 64 — ambos resolvidos na compilação.
// Feature: comptime stdlib bits — string methods and `math.*` work too
// Syntax: same `comptime { ... }` block; you can call a curated subset
// of stdlib helpers (string trim/upper, math.sqrt, etc.).
// When to use: bake banner strings, compile-time constants from
// numerical formulas, normalize identifiers at build time.
use std::math
let banner = comptime {
let raw = " Hello, Comptime! "
raw.trim().to_upper()
}
print(banner)
// expected: HELLO, COMPTIME!
let r = comptime math.sqrt(64.0)
print(r)
// expected: 8
Incorporar arquivos com comptime fs.read
comptime fs.read("caminho") lê um arquivo durante a compilação e substitui
a chamada pela string resultante. O caminho é sempre relativo ao arquivo
fonte, não ao diretório de trabalho do compilador.
Use essa funcionalidade para embutir templates, prompts, esquemas JSON, queries
SQL ou shaders diretamente no binário. Não há fopen em produção, não há risco
de arquivo ausente em implantação e não há trabalho de resolução de caminhos
em tempo de execução.
O resultado é uma string comptime normal — você pode encadeá-lo com outros métodos comptime na mesma expressão.
Leitura de arquivo em tempo de compilação e transformação da string resultante.
// Feature: bake a file's contents into the program at compile time,
// reusing the runtime `std::fs` API under a `comptime` prefix.
// Syntax: `comptime fs.read("relative/path")` reads the file during
// compilation and replaces the call with the resulting string literal.
// Path resolution in comptime is **always relative to this source
// file**, not the compiler's CWD — see `specs/comptime-fs.md`.
// When to use: ship templates, prompts, JSON schemas, SQL queries, or
// shaders as string constants — no runtime fopen, no missing-file
// failures in production, no path resolution gymnastics.
use std::fs
let banner = comptime fs.read("07-embed-file.banner.txt")
print(banner)
// expected (once implemented):
// Welcome to Zolo!
// comptime makes constants free.
// You can pipe the result through other comptime methods, since the
// return is a normal comptime string.
let upper_banner = comptime {
fs.read("07-embed-file.banner.txt").trim().to_upper()
}
print(upper_banner)
// expected (once implemented):
// WELCOME TO ZOLO!
// COMPTIME MAKES CONSTANTS FREE.
Veja também