Here are quick notes i wrote down, to remember nim-basics,
Maybe it can also help you,

nim-tutorial

Nim has a concise and elegant syntax.

Its syntax, concise and elegant, is maybe inspired by Haskell’s one.

But Nim is an imperative language, while Haskell is purely functional.

Let’s try to make a "hello-world" example with Nim:

echo "hello"

You can compile, and run this example as follows:

$ nim c hello.nim
$ ./hello
hello

The nim-compiler can be installed with the command:

$ sudo apt-get install nim

Nim sources can be transpiled to c and javascript, with respectively the commands nim c, and nim js.

It is also possible to compile and run at the same time with the additional parameter -r:

$ nim c -r hello.nim
hello

We can create an object with several fields with a type declaration:

type
  Spell = object
    name: string
    manaCost: int

let spell = Spell(name: "Harmonic Lantern", manaCost: 36)

proc printSpell(spell: Spell) =
  echo "spell: ", spell.name
  echo "mana-cost: ", spell.manaCost

printSpell(spell)

Let’s compile, and run:

$ nim c spell.nim
$ ./spell
spell: Harmonic Lantern
mana-cost: 36

Nim transpiles to c and javascript. When compiled to a binary executable, the intermediate c code can be found in ~/.cache/nim/.


If you transpile to javascript, and if you got node installed, you can also run the javascript result as if it were a script:

$ nim js spell.nim
$ node spell.js
spell: Harmonic Lantern
mana-cost: 36

The node command can be installed with:

$ sudo apt-get install nodejs

sequences

Here is how to define, and add an element to a sequence:

var sq = @[1, 2, 3]
sq.add(4)
echo sq

Result:

$ nim c -r sq.nim
@[1, 2, 3, 4]

It is also possible to insert an element at the beginning, with the function insert() and the index 0:

var sq = @[3, 4, 5]
sq.insert(2, 0)
echo sq
$ nim c -r sq0.nim
@[2, 3, 4, 5]

seq-rem-last-element

We can use the pop() function to remove and return the last element of a sequence:

var sq = @[1, 2, 3, 4]
discard sq.pop()
echo sq

Then use the keyword discard to ignore the returned element, if you don’t need it.

Result:

@[1, 2, 3]

functions-returning-several-elements

proc myFunction(): (int, string) =
  let number = 56
  let text = "Example"
  return (number, text)

let (n, s) = myFunction()
echo n, " ", s

let-and-var-variable-declaration

Nim variables can be declared with let and var keywords.
let will create an immutable variable, and
var will create a variable that you can modify later.


random-numbers

We can get random numbers by calling the function rand().
The bounds of the range provided as the paramter are included for the results:

import random

randomize()  # initialises the seed

var sq: seq[int] = @[]

for i in 0 .. 20:
  sq.add(rand(1..8))

echo sq

If you forget to initialise the seed of the random number generator, the sequence will always be the same.

$ nim c -r r.nim
@[2, 1, 8, 8, 6, 8, 3, 7, 1, 3, 8, 5, 2, 5, 8, 8, 4, 6, 5, 6, 2]

nimble-package-manager

Nim’s package-manager is called "nimble":

You can check the list of available packages with the command:

$ nimble list | less

And if there is one you would like to try, you can use the command:

$ nimble install foo
Downloading https://github.com/someone/foo using git
  Verifying dependencies for foo@0.01
Installing foo@0.01
  Success: foo installed successfully.

The foo package will be installed in ~/.nimble/pkgs/, probably in a sub-directory called foo-0.01/.

You will then find the uncompiled source of this lib, as the file foo-0.01/foo.nim, so you can open it with a text-editor, if you want to check for some details.


© 2025 fccm — license: cc-by-sa_2.5