Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 36 ยท Getting Started

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.

Nim is a statically typed, compiled programming language with a clean, indentation-based syntax. If you have read Python, a Nim program looks familiar on the first screen. But underneath it behaves like C: it is translated to machine code ahead of time, it has no virtual machine, and a small program becomes a small native executable.

The language was created by Andreas Rumpf and first released in 2008 (under the name Nimrod). Today it is developed in the open by a community of contributors. The goal is easy to state: be as expressive as a scripting language, as fast as a systems language, and flexible enough that you can reshape the language itself with macros.

Nim cannot run inside this website. Every Nim snippet in this course is shown read-only, and I only print output when I am certain of it. Where an idea can be demonstrated with real running code, I give a clearly labelled C stand-in that you can edit and run on the page. Install Nim on your own computer (next lessons) to run the real thing.

How Nim works

The Nim compiler (nim) does not generate machine code directly. It translates your .nim files into C (or C++, Objective-C or JavaScript), then calls a C compiler such as GCC or Clang to produce the final program. This is why Nim programs are fast, why they can call any C library with almost no glue, and why they run on anything a C compiler supports.

flowchart LR
  A["hello.nim<br/>your source"] --> B["Nim compiler<br/>parse, type-check, optimize"]
  B --> C["generated C<br/>in nimcache"]
  C --> D["C compiler<br/>gcc / clang"]
  D --> E["native executable"]
  B --> F["JavaScript<br/>(js backend)"]

Caption: the default backend turns Nim into C and lets a C compiler do the last step; the JS backend skips C entirely.

A first look

Here is a small Nim program. Do not worry about every detail yet; the next lessons explain each piece.

NIM
# Sum the squares of 1..10 and print a message
var total = 0
for i in 1 .. 10:
  total += i * i

echo "Sum of squares: ", total

var declares a variable that can change, for i in 1 .. 10 loops over an inclusive range, blocks are marked by indentation (no braces), and echo prints its arguments followed by a newline. The type of total is inferred as int; you never had to write it. The sum is 385.

What makes Nim different

Why people pick Nim

Readable Indentation syntax
Fast Compiles to native code
Flexible Templates and macros
Connected Calls C and C++ easily

One language for scripts, tools, servers and libraries.

1 / 3

Key traits

  • Static typing with inference. The compiler checks types before the program runs, but you only write them where it matters (procedure signatures, mostly).
  • Value semantics by default. Assigning an object copies it. Sharing is something you ask for with ref or ptr.
  • Compile-time execution. Constants, static blocks and macros run at compile time, so work moves out of your running program.
  • Memory management you can reason about. Since Nim 2.0 the default is ORC, a reference-counting scheme with a cycle collector. There is no stop-the-world garbage collector pause.
  • Style-insensitive identifiers. myValue, my_value and myvalue all name the same thing (only the first letter is case-sensitive). The community convention is camelCase.
  • Small output. A hello-world binary is a few hundred kilobytes, or much less with tweaks.

Where Nim is used

Command-line tools, game and graphics code, servers and web back ends, embedded and microcontroller firmware, scientific scripts, and wrappers around existing C libraries. It is also a good first compiled language, because the syntax gets out of your way.

A stand-in: what the generated C looks like

You can think of the Nim loop above as roughly this C code. The real generated code has extra names and checks (for example, integer overflow checks in debug builds), but the shape is the same. This is a C stand-in, not Nim.

C Stand-in: the sum-of-squares loop as C

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Nim's int is as wide as a pointer, so it is 64 bits on 64-bit machines. That is why the stand-in uses long long.

How this course is organized

You will start with installation and your first program. Then you cover variables and types, procedures and control flow, and the built-in collections. Later chapters cover objects and generics, templates and macros, memory and concurrency, and talking to C. The course ends with projects.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How does the default Nim backend produce an executable?

    Nim generates C (or C++/Objective-C) source and calls a C compiler. The JavaScript backend is the exception.
  2. 2. How are blocks of code grouped in Nim?

    Nim uses indentation like Python. Braces are used for other things such as table and set constructors.
  3. 3. Which statement about Nim's identifiers is true?

    Nim is style-insensitive: the first character is case-sensitive, later case and underscores are ignored when comparing names.
  4. 4. Why does this course use C stand-ins?

    Nim code here is read-only. C code runs on the site's server runner, so it demonstrates the same ideas.

Summary

  • Nim is a statically typed compiled language with Python-like, indentation-based syntax.
  • It compiles to C by default (also C++, Objective-C and JavaScript) and is therefore fast and C-friendly.
  • It has value semantics, compile-time execution, macros and ORC memory management.
  • Identifiers are style-insensitive; camelCase is the convention.

Next: Installing Nim and Nimble