liveBook · Manning

@embedFile

As explained in Systems Programming with Zig

@embedFile is a Zig compile-time builtin that embeds a file's contents into a program as a byte slice.

@embedFile("words.txt") evaluates to the file’s contents as though they were copied into the source. Pass those contents to a compile-time parser to turn a wordlist into program data. section 4.1.3

Listing 4.6 Using our parser
const words = blk: { #1
    @setEvalBranchQuota(1_000_000); #2
    break :blk parseWordlist(@embedFile("words.txt")); #3
};

The listing embeds words.txt, raises the compile-time branch quota, and passes the contents to the wordlist parser. section 4.1.3

How Systems Programming with Zig covers it

How deep it goes
Defines embedded contents as source-equivalent text and demonstrates parsing them into a fixed-size array.
Examples
Uses words.txt as a four-character wordlist, parses it into an array, and inspects the result with @compileLog.
What you can do after
Build it from scratch: embed words.txt and pass its contents to a compile-time wordlist parser.
How it connects
Connects compile-time evaluation, compile-time parsing, and @setEvalBranchQuota.

This explainer was generated by Manning's knowledge graph from the text of Systems Programming with Zig. The section links point to the parts of the book it draws on.