Nim Demo Examples

Practice programs, one file each. Every demo is self-contained — copy it, modify it, run it. Each one imports only the standard library, so nothing else needs installing.

How to use: click View to open a file in the code viewer, read its comments, download it to your machine, and compile it with the Nim toolchain. Most demos need only nim c -r <file>; the threads demo needs the default --mm:orc, and the build-modes demo is worth running three times with different defines.

nim c -r 01_hello.nim                       # compile and run (debug build)
nim c -r -d:release 19_buildmodes.nim       # same source, optimized build
nim c -r 15_threads.nim                     # threads are on by default in Nim 2.x
nim c -r 16_interop.nim                     # links against libc (math.h, stdlib.h)

Foundations

Values, control flow and routines — the whole of PHASE 1 and PHASE 2.

#DescriptionLink
1Hello, World and echoView
2Values, Variables and TypesView
3Selection and IterationView
4Procedures and First-Class FunctionsView

Data Modelling

Objects, variants, containers and text — PHASE 3, where data structures become the design.

#DescriptionLink
5Objects and Dynamic DispatchView
6Enums, Sets and Variant ObjectsView
7Arrays, Sequences, Sets and TablesView
8Strings, Characters and FormattingView

Abstraction & Reuse

Generics, modules, iterators and metaprogramming — PHASE 4, the machinery that lets one program serve many cases.

#DescriptionLink
9Generics and Type ClassesView
10Modules and ImportsView
11Inline and Closure IteratorsView
12Templates and MacrosView

Safety & Performance

Errors, memory, concurrency and the C boundary — PHASE 5, where a correct program becomes a fast, reliable one.

#DescriptionLink
13Exceptions, defer and OptionView
14Value vs Reference SemanticsView
15Threads, Channels and AsyncView
16C Interop and Static LibrariesView

Engineering Practice

Tests, file I/O, build modes and project configuration — PHASE 6, how the work ships.

#DescriptionLink
17Unit Tests with std/unittestView
18Files, Paths and Command-Line OptionsView
19Build Modes and Runtime ChecksView
20Project Configuration (config.nims)View