Learn Vex through examples
Choose a task, then follow the linked program and the API contracts around it. The examples cover data modeling, resource ownership, concurrent work, compile-time programming, numerical code and application libraries.
Complete programs identify their filename, run command and expected behavior. Library fragments show a component to embed in a larger application; keep the surrounding imports, owners and error handling when adapting them.
Start with a small application
| Task | What you practice | Example |
|---|---|---|
| Build a two-file library and its tests | Exported interfaces, imports, immutable/mutable values and test discovery | Your first project |
| Calculate a credit with rejected inputs | A domain contract, typed failures and positive/negative tests | Testing and benchmarking |
| Return either a value or a useful error | Result, match, early return and exit status | Introduction, error handling |
| Build a command-line interface | Commands, flags, arguments and help text | CLI library |
| Read structured configuration | Owning parsed values, lookup and conversion errors | JSON, TOML, YAML |
Make ownership visible
| Task | What you practice | Example |
|---|---|---|
| Validate a packet and return a borrowed payload | Bounded views, error paths and keeping the owner alive | Ownership workflows |
| Build an independently owned report | Borrowed input, transformation and owned return values | Ownership workflows |
| Call a small C function | A native header/provider, ABI selection and one cleanup owner | Your first native call |
| Work with file contents or mapped regions | I/O errors, resource cleanup, bytes and filesystem paths | Files, filesystem API |
| Protect and authenticate a message | Key and nonce lifecycle, byte lengths and authentication failure | Encryption |
Coordinate independent work
| Task | What you practice | Example |
|---|---|---|
| Collect a batch containing successes and failures | Async jobs, tagged replies, Result and bounded channels | Fallible task results |
| Merge producers and shut down cleanly | Fan-in, completion counts, channel ownership and cancellation | Fan-in |
| Serve HTTP requests | Request/response ownership, routes, middleware and body limits | Fiber service, middleware |
| Store and query application state | Connection/statement lifetimes, parameter binding and transaction scope | Database connections, ORM |
Move work into the compiler
| Task | What you practice | Example |
|---|---|---|
| Calculate a bounded retry schedule | Const functions, strict compile-time values and overflow checks | Compile-time recipes |
| Process fields selected by their exact type | Typed reflection and statically checked field access | Compile-time recipes |
| Generate repeated declarations from a typed description | DeclSet, generated methods/types and semantic checking | Structural generation |
| Share serialization metadata across models | Named policies, field tags, overrides and reflection | Policies |
Work with numbers, images and protocols
| Task | What you practice | Example |
|---|---|---|
| Calibrate a batch of sensor values | Multiple graph inputs, elementwise arithmetic, masks and reduction | Graph functions |
| Differentiate a numerical expression | Primal computation, forward-mode derivatives and sensitivities | Autograd |
| Process a tile and its boundary | Shape/index arithmetic and a scalar reference | Fusion and tiled work |
| Measure a geographic journey or region | Coordinate order, units, distance and containment | Geographic coordinates |
| Transform an image in memory | Pixel layout, owning images and deterministic transforms | Image processing |
| Create PCM and describe it with a WAV header | Sample layout, buffer sizes and container metadata | Media components |
| Inspect and reconstruct an RTP packet | Checked parsing, network byte order and packet bounds | Real-time protocols |
| Model a FIFO | Capacity, occupancy, simultaneous operations and parameter widths | Hardware examples |
The hardware example belongs to the companion VexHDL project. GPU execution, native libraries and network peers each introduce their own environment; follow the linked page's setup and ownership contract.
Build a language tool
The language-tooling guide includes complete tokenizer and width-sensitive layout programs, a lossless syntax-tree fragment, and a map of the ten IDE library families. Extend those building blocks with grammar rules, source-labelled diagnostics, query metadata or editor messages.
When a compiler change needs a regression, use compiler testing tools to retain and replay generated cases. Application benchmarks and compiler-phase benchmarks measure different workloads; keep their inputs and results separate.
Adapt an example without losing its contract
Change one dimension at a time: input size, error case, ownership boundary, concurrency or target. Keep an expected result for each change. For a parser, include empty/truncated input; for a pipeline, include shutdown; for a numerical operation, compare to a reference with an explicit tolerance.
The capability map shows the broader language surface. The verification guide explains how to retain the command, compiler identity and result for your adaptation.