Nim Basics — Syntax, Variables, Types, and Compilation
In this tutorial, you will learn about Nim Basics. We cover key concepts, practical examples, and best practices to help you master this topic.
Nim is a systems programming language that combines Python's readability with C's performance. It compiles to C, C++, or JavaScript, giving you low-level control without sacrificing expressiveness.
In this tutorial, you'll learn Nim's core syntax and execution model. Nim is used at DodaTech for performance-critical security tools where Python is too slow but C is too verbose.
What You'll Learn
- Installing Nim and compiling programs
- Variables and type inference
- Basic types
- Compilation modes (debug, release)
- Input and output
Hello, Nim
echo "Hello, Nim!"
# Compile and run:
# nim c hello.nim && ./hello
Variables
# Immutable by default (let)
let name = "Alice"
# name = "Bob" # Error: cannot assign to let
# Mutable variables (var)
var age = 30
age = 31 # OK
# Explicit types
let country: string = "India"
var count: int = 0
let pi: float64 = 3.14159
Basic Types
# Integers
let
a: int8 = 127
b: int16 = 32767
c: int32 = 2147483647
d: int64 = 9223372036854775807
e: uint = 42 # Unsigned
# Floats
let
f: float32 = 1.5
g: float64 = 3.141592653589793
# Characters and strings
let
ch: char = 'A'
s: string = "Hello"
multi = """Multi-line
string"""
# Booleans
let
flag: bool = true
another = false
# Type conversions
int("42") # => 42
float(3) # => 3.0
$42 # => "42" (stringify)
Control Flow
# if/elif/else
let score = 85
let grade = if score >= 90:
"A"
elif score >= 80:
"B"
else:
"F"
echo grade # B
# case
let status = case score:
of 90..100: "Excellent"
of 80..89: "Good"
of 70..79: "Fair"
else: "Needs improvement"
# loops
for i in 0..4:
echo i # 0, 1, 2, 3, 4
for item in ["Alice", "Bob", "Charlie"]:
echo item
var i = 0
while i < 5:
echo i
inc i
Procedures
# Functions are called procedures
proc add(a, b: int): int =
return a + b
# Implicit return (last expression)
proc multiply(a, b: int): int =
a * b
# Default arguments
proc greet(name: string = "World"): string =
"Hello, " & name & "!"
echo greet() # Hello, World!
echo greet("Nim") # Hello, Nim!
# Multiple return values
proc divide(a, b: int): (int, int) =
(a div b, a mod b)
let (q, r) = divide(10, 3)
echo q # 3
echo r # 1
Compilation
# Debug build (fast compilation, slow execution)
nim c hello.nim
# Release build (slow compilation, fast execution)
nim c -d:release hello.nim
# Run without producing binary
nim r hello.nim
# Compile to JavaScript
nim js hello.nim
Practice Questions
Write a procedure that takes two integers and returns their sum, difference, and product.
Use a for loop to print the first 20 Fibonacci numbers.
Write a case statement that categorizes an integer as single-digit, double-digit, or triple-digit.
Create a procedure with a default parameter value.
Write a program that reads a name from stdin and prints a greeting.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro