Skip to content
Tutorials Center
My learning

Topics

Nim Tutorial

Learn Nim step by step: 36 lessons in 9 chapters with examples, quizzes, diagrams and projects.

Start learning →

of lessons completed

Getting Started

  1. 1 What is Nim? Meet Nim, a statically typed language with Python-like syntax that compiles to C, C++ or JavaScript. See where it shines, how it works and what this course covers.
  2. 2 Installing Nim and Nimble Install the Nim compiler and the Nimble package manager on Windows, macOS or Linux, check your setup, pick an editor and understand version requirements.
  3. 3 Hello, Nim: Compile and Run Write and compile your first Nim program, learn nim c -r, release builds, backends, comments, echo, reading input and command-line arguments, with C stand-ins.

Basics and Types

  1. 4 Variables: let, var and const Learn Nim's three ways to declare names (let, var, const), type inference, default values, scope, shadowing, tuple unpacking and naming rules, with C stand-ins.
  2. 5 Numbers: Integers and Floats Learn Nim's integer and float types, literals, div and mod versus slash, shifts, overflow checking, the math module and rounding, with runnable C stand-ins.
  3. 6 Booleans, Characters and Operators Learn Nim's bool and char types, and, or, not, short-circuit logic, comparison, the in operator with sets, string concatenation, precedence rules and custom operators.
  4. 7 Type Conversions and Distinct Types Convert between Nim types safely with int(), float(), $ and parseInt, understand cast, make type aliases, and use distinct types to stop unit mix-ups at compile time.
  5. 8 Enums and Subranges Define Nim enums, iterate over them, give them string values, use pure enums, restrict values with range types, and store flags with bit sets, with C stand-ins.

Procedures and Control Flow

  1. 9 Decisions: if, case and when Learn Nim conditionals: if/elif/else as statements and expressions, case with ranges and strings, and the compile-time when statement with defined(), with C stand-ins.
  2. 10 Loops and Ranges: for, while, break Learn Nim loops: for over ranges and collections, countup and countdown, while, break, continue, labeled block exits, enumerate-style indexing and nested loops.
  3. 11 Procedures: proc, func and result Define procedures in Nim with proc and func, return values with result and return, use default and named arguments, discard results, forward declarations, with C stand-ins.
  4. 12 Parameters, Overloading and UFCS Understand how Nim passes arguments, mutate callers' values with var parameters, accept lists with openArray and varargs, overload procedures, and chain calls with UFCS.
  5. 13 Iterators, Closures and Lambdas Write your own Nim iterators with yield, understand inline and closure iterators, create closures that capture variables, use lambdas and the sugar module, with C stand-ins.
  6. 14 Exceptions, try/except and defer Handle errors in Nim with try/except/finally, raise your own exceptions, tell Defects from CatchableErrors, clean up with defer, and use assert, with C stand-ins.

Collections and Strings

  1. 15 Arrays and Sequences (seq) Learn Nim's fixed-size arrays and growable seq: literals, indexing, slices, ^ indexes, add, insert, delete, sorting, map and filter, copying rules and bounds errors.
  2. 16 Tables and Sets: Hash Maps in Nim Use Nim's std/tables and std/sets: create tables, look up keys safely, count items with CountTable, keep insertion order, and do union and intersection with HashSet.
  3. 17 Tuples and Option: Grouping and Maybe-Values Group values with Nim tuples, return several results from a procedure, unpack them, and model a missing value safely with std/options instead of nil or magic numbers.
  4. 18 Strings, strutils, strformat and Runes Work with Nim strings: bytes versus characters, slicing, concatenation, strutils helpers, split and join, strformat formatting, multi-line strings and Unicode with std/unicode.

Objects and Generics

  1. 19 Objects and Tuples in Nim Learn how Nim groups data with object, ref object and tuple types, how construction and field access work, and why value and reference objects behave differently.
  2. 20 Inheritance and Methods in Nim Learn single inheritance with of RootObj, how Nim's method keyword dispatches at runtime, the difference from proc, and how a vtable makes it work underneath.
  3. 21 Variant Objects and Enums in Nim Learn Nim enums, sets of enums and variant objects (case objects) that store different fields per case, plus how they map to tagged unions in memory.
  4. 22 Generics and Concepts in Nim Learn generic procs and types in Nim, how type parameters are instantiated at compile time, constraints, and concepts, with a C stand-in showing monomorphization.

Macros and Templates

  1. 23 Templates in Nim Learn how Nim templates work as hygienic code substitution, untyped and typed parameters, passing blocks, and building your own control structures such as withLock.
  2. 24 Macros and the AST in Nim Learn what Nim macros are, how code becomes an abstract syntax tree, how to build new code with quote do, and how a tree evaluator works, with runnable C stand-ins.
  3. 25 Compile-Time Execution in Nim Learn how Nim runs code during compilation with const, static blocks, when and compileTime, to precompute tables and select code per platform, with C stand-ins.

Memory and Concurrency

  1. 26 Memory Management with ARC and ORC in Nim Learn how Nim frees memory without a tracing garbage collector, how ARC and ORC use reference counting and destructors, and what move semantics mean, with C stand-ins.
  2. 27 Value, Reference and Pointer Semantics in Nim Learn how Nim passes arguments, what var parameters do, the difference between ref and ptr, when to use addr and unsafe pointers, and how openArray avoids copies.
  3. 28 Threads, Channels and Locks in Nim Learn how Nim creates threads, passes messages through channels, protects shared data with locks and atomics, and avoids data races, with pthread C stand-ins.
  4. 29 Async and Await in Nim Learn Nim's asyncdispatch module: async procs, await, futures, running several tasks at once with all, and how an event loop works, with a C stand-in scheduler.

FFI and Tooling

  1. 30 Calling C from Nim with importc Learn Nim's foreign function interface: importc and header pragmas, cstring and C types, structs and callbacks, linking libraries, and exporting Nim procs to C.
  2. 31 Nimble, Packages and Project Layout in Nim Learn how Nimble creates projects, declares dependencies and tasks in a .nimble file, installs packages, structures a Nim project, and how version ranges are resolved.
  3. 32 Testing and Debugging Nim Programs Learn to test Nim code with std/unittest, assert and doAssert, use compiler flags such as --checks and -d:release, debug with echo and gdb, and read stack traces.

Projects and Practice

  1. 33 Project: A Word Frequency Counter in Nim Build a word frequency counter in Nim using strings, tables and sorting, then follow the same algorithm in a runnable C version with a hash table.
  2. 34 Project: A Tokenizer and Calculator in Nim Build an expression calculator in Nim with a tokenizer using variant objects and a recursive descent parser, plus a runnable C version of the same design.
  3. 35 Project: A Generic Ring Buffer in Nim Build a fixed-size generic ring buffer in Nim with static sizes, iterators and operator templates, and try the same circular-queue logic in runnable C.
  4. 36 Nim Practice Problems: Objects, Memory and Tooling Test what you learned in Nim with practice problems on objects, variants, generics, templates, memory, concurrency and FFI, with hints and runnable C checks.