Skip to content

Nim Data Types — Strings, Arrays, Sequences, Sets, and Tables

DodaTech Updated 2026-06-29 2 min read

In this tutorial, you will learn about Nim Data Types. We cover key concepts, practical examples, and best practices to help you master this topic.

Nim offers rich built-in data types that combine convenience with performance. Its generic arrays and sequences match C's memory layout while providing Ruby-like ergonomics.

In this tutorial, you'll explore Nim's core data structures and when to use each.

What You'll Learn

  • Strings (mutable, with interpolation)
  • Arrays (fixed-size, stack-allocated)
  • Sequences (dynamic arrays)
  • Sets for membership tests
  • Tables (hash maps)
  • Tuples and object types

Strings

let name = "Nim"
echo "Hello, ", name, "!"    # Concatenation
echo "Hello, " & name & "!"  # Also works
echo "Length: ", name.len    # => 3

# Interpolation using strformat
import strformat
echo fmt"Hello, {name}!"     # => Hello, Nim!

# Mutable strings
var s = "hello"
s[0] = 'H'     # Mutable!
s.add(" world")
echo s  # => Hello world

# Multi-line
let text = """Line one
Line two
Line three"""

Arrays (Fixed-Size)

# Stack-allocated, fixed size
let arr: array[5, int] = [1, 2, 3, 4, 5]
echo arr[0]  # => 1
echo arr.len  # => 5

# Type inference
let colors = ["red", "green", "blue"]

# 2D array
let matrix: array[3, array[3, int]] = [
  [1, 0, 0],
  [0, 1, 0],
  [0, 0, 1]
]

Sequences (Dynamic Arrays)

# Heap-allocated, resizable
var numbers = @[1, 2, 3]  # @ makes a seq
numbers.add(4)
numbers.add(5)
echo numbers  # => @[1, 2, 3, 4, 5]

# Operations
numbers.delete(0)        # Remove by index
numbers.insert(0, 0)     # Insert at index
echo numbers.len          # => 5
echo numbers[0..2]        # Slice: @[0, 2, 3, 4]

# Map/filter
import sequtils
let doubled = numbers.mapIt(it * 2)
let evens = numbers.filterIt(it mod 2 == 0)

Sets

# Character sets
let vowels: set[char] = {'a', 'e', 'i', 'o', 'u'}
echo 'a' in vowels  # => true
echo 'z' in vowels  # => false

# Integer sets (limited to small ranges)
let primes: set[int8] = {2, 3, 5, 7, 11, 13}

# Set operations
let a = {1, 2, 3, 4}
let b = {3, 4, 5, 6}
echo a + b   # Union: {1, 2, 3, 4, 5, 6}
echo a * b   # Intersection: {3, 4}
echo a - b   # Difference: {1, 2}

Tables (Hash Maps)

import tables

# Mutable table
var user = initTable[string, string]()
user["name"] = "Alice"
user["role"] = "admin"
echo user["name"]  # => Alice

# Immutable (constructor)
let config = {"port": "8080", "host": "localhost"}.toTable

# Check existence
if user.hasKey("email"):
  echo user["email"]
else:
  echo "No email"

# Default value
echo user.getOrDefault("email", "not set")

Practice Questions

  1. Create a sequence of strings, add 5 items, remove the 3rd, and iterate over the result.

  2. Use a set to find all vowels in a given string.

  3. Build a table mapping country names to their capitals.

  4. Write a procedure that takes a sequence of integers and returns a new sequence with duplicates removed (use a set).

  5. Use array of arrays to represent a 3x3 Tic-Tac-Toe board.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro