Damien Gonot
Home Blog Notes About

Nim

Homepage / Notes / Computer Science / Programming Languages / Nim

https://nim-lang.org/

Fast general purpose programming language

Language Features

Basics

echo "Hello World!"
Hello World!
echo 1+1
2

Variables

var letter = 'a'
echo letter
a

With type declaration:

var lang: string = "Nim"
echo lang
Nim

let for immutables:

let name: string = "Damien"
echo name
Damien

const for immutables known at compile-time:

const number: int = 9
echo number
9

Strings

Concatenation:

var name = "Damien"
var greeting = "Hello "
echo greeting & name
Hello Damien

Containers

Arrays

echo ['a', 'b', 'c', 'd', 'e']
['a''b''c''d''e']
let list = ['a', 'b', 'c', 'd', 'e']
echo list[1]
echo list[^1]
b
e

Countup

for i in countup(1, 5):
  echo i
1
2
3
4
5

Shorter syntax for countup():

for i in 1 .. 5:
  echo i
1
2
3
4
5

Control Flow

if 1 < 2:
  echo "Math is right"
Math is right

For Loops

for letter in "Damien":
  echo letter
D
a
m
i
e
n
for index, letter in ['a', 'b', 'c']:
  echo index, " ", letter
0a
1b
2c

Resources