Beginner

Hello World in Nim

Your first Nim program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Nim program.

The Code

Create a file named hello.nim:

1
echo "Hello, World!"

That’s it! If you know Python, this looks very familiar - and that’s by design.

Understanding the Code

  • echo - Prints values to standard output followed by a newline
  • "Hello, World!" - A string literal enclosed in double quotes
  • No parentheses required - Nim allows calling procedures without parentheses
  • No main function - Top-level code runs automatically

Running with Docker

The easiest way to run this without installing Nim locally:

1
2
3
4
5
# Pull the official Nim image
docker pull nimlang/nim:alpine

# Run the program (compile and execute)
docker run --rm -v $(pwd):/app -w /app nimlang/nim:alpine nim c -r hello.nim

Running Locally

If you have Nim installed:

1
2
3
4
5
6
# Compile and run in one step
nim c -r hello.nim

# Or compile to a binary first
nim c hello.nim
./hello

Expected Output

Hello, World!

Key Concepts

  1. Python-like syntax - Clean, readable code without excessive punctuation
  2. Compiled language - Despite the scripting feel, Nim compiles to efficient native code
  3. nim c - Compiles to C backend (default)
  4. -r flag - Run the compiled binary immediately after compilation
  5. No boilerplate - Jump straight to writing code

Understanding Compilation

Nim compiles through an intermediate step:

1
2
3
4
5
6
# Compile to C, then to native binary
nim c hello.nim

# See the generated C code
nim c --nimcache:./nimcache hello.nim
ls nimcache/

This two-stage compilation lets Nim leverage decades of C compiler optimizations.

A More Detailed Version

For a slightly more structured approach showing Nim’s type system:

1
2
3
4
5
6
7
# A typed greeting procedure
proc greet(name: string): string =
  result = "Hello, " & name & "!"

# Call the procedure and print the result
let message = greet("World")
echo message

This version demonstrates:

  • Procedure definition - Using proc with type annotations
  • Type annotations - name: string and return type : string
  • String concatenation - Using the & operator
  • result variable - Nim’s implicit return variable
  • let binding - Immutable variable declaration

Object-Oriented Approach

Nim supports object-oriented programming:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
type
  Greeter = object
    name: string

proc newGreeter(name: string): Greeter =
  Greeter(name: name)

proc greet(g: Greeter) =
  echo "Hello, ", g.name, "!"

let greeter = newGreeter("World")
greeter.greet()

Key features shown:

  • type section - Defines custom types
  • object type - Nim’s structure/record type
  • Constructor pattern - newGreeter returns an initialized object
  • Method syntax - greeter.greet() is syntactic sugar for greet(greeter)

Compilation Options

Nim offers various compilation modes:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# Debug build (default) - faster compilation
nim c hello.nim

# Release build - optimized for speed
nim c -d:release hello.nim

# Danger mode - maximum speed, no safety checks
nim c -d:danger hello.nim

# Check for errors without generating binary
nim check hello.nim

Multiple Output Formats

Nim can compile to different backends:

1
2
3
4
5
6
7
8
# Compile to C (default)
nim c hello.nim

# Compile to C++
nim cpp hello.nim

# Compile to JavaScript
nim js hello.nim

The JavaScript target creates hello.js that runs in browsers or Node.js.

Why Nim Stands Out

Coming from other languages, you’ll notice Nim’s unique characteristics:

  • Python syntax, C speed - Write readable code that runs fast
  • Multiple backends - One codebase, multiple targets (C, C++, JS)
  • Powerful metaprogramming - Macros that operate on the AST
  • Modern memory management - ORC provides deterministic destruction

Next Steps

Continue to Variables and Data Types to learn about Nim’s powerful type system, including distinct types, type inference, and the let/var/const distinction.

Running Today

All examples can be run using Docker:

docker pull nimlang/nim:alpine
Last updated: