Odin Lab Examples

Seventeen single-file programs, grouped by phase. Each one is self-contained: it imports only the standard library, so nothing beyond the compiler has to be installed, and every file carries its own comments explaining what to look at.

How to use this page: click View to open a file in the code viewer, read it, download it, and run it with the Odin toolchain. Every demo is designed to be edited — change a value, re-run, and watch the output move.

odin run 01_hellope.odin -file      # compile and run one file
odin check .                        # type-check a folder, no binary produced
odin test .                         # run the @(test) procedures in a package
odinfmt -w 01_hellope.odin          # rewrite a file in the official style

Two notes about those commands. The -file flag is what tells the compiler to treat a single file as its own package — without it, odin expects a directory. And odin test works on a folder, which is why the testing demo asks you to copy it into a folder of its own first.

Foundations

PHASE 1 — the shape of a program, declarations, scope, and the defer habit that follows you through the whole track.

#DescriptionLink
1Hello, odin! — the smallest complete programView
2Values and Scope — declarations, mutability, constants, deferView

Types & Control

PHASE 2 — the type families and their sizes, the operators including the bitwise family, decisions, and every form of for.

#DescriptionLink
3Types — families, widths, conversions, casts, distinct typesView
4Operators — arithmetic, comparison, logic, and the bitsView
5Control and Loops — if, switch, four for shapes, labelled breakView

Composition

PHASE 3 — procedures with several results, the four data shapes, and containers that copy, view or own their memory.

#DescriptionLink
6Procedures — results, named returns, defaults, deferView
7Structs, Enums, Bit Sets and Unions — the four data shapesView
8Collections and Maps — array, slice, dynamic array, mapView

Memory & Data

PHASE 4 — addresses and dereferencing, the allocator discipline that replaces a garbage collector, and the layout decisions that decide how fast a loop runs.

#DescriptionLink
9Pointers — addresses, dereferencing, nil, and editing in placeView
10Allocators — make, new, delete, free, and the temporary allocatorView
11Data-Oriented Design — layout, cache-friendly loops, flags, timingView

Reliability & Metaprogramming

PHASE 5 — errors as values, one body for many types, decisions made while compiling, and the standard packages you reach for daily.

#DescriptionLink
12Errors — the two-value shape, or_return, or_elseView
13Generics — $T parameters, where clauses, a generic stackView
14Compile-time Programming — when, #assert, ODIN_OS, ODIN_DEBUGView
15Core Packages — imports, aliases, os.args, sorting, building stringsView

Interop & Tooling

PHASE 6 — threads that run alongside the main one, and the tests that keep yesterday's answers true tomorrow.

#DescriptionLink
16Threads — create, pass data through the handle, start, joinView
17Testing — @(test) procedures, expect, expect_value, memory trackingView

How to Practise With These

A demo read once teaches very little, so here is a sequence that teaches more than the reading does. Take one file per sitting, in order.

First: run it unchanged and read the output. Second: predict one change before making it — a different value, a different type, a loop that runs twice as long — and see whether you were right. Third: break it on purpose: remove a defer delete, dereference a nil pointer, pass a float where an integer is expected. The compiler's messages are part of the lesson, and reading them is a skill of its own.

When a demo is too easy, edit it into something you actually want: a word counter, a rectangle checker, a tiny file inspector. The language only becomes yours when the program stops being an exercise.