Nim's hash table module for key-value storage
tables ModuleThe tables module provides hash table implementations for key-value storage in Nim.
Table[A, B] - Standard hash table (value semantics, copies on assignment)TableRef[A, B] - Reference-based hash table (shared on assignment)OrderedTable[A, B] - Preserves insertion orderCountTable[A] - Maps keys to occurrence countsimport std/tables
# Create empty table
var t = initTable[string, int]()
t["key"] = 42
# Create from pairs literal
let t2 = {"a": 1, "b": 2}.toTable
# TableRef for ref semantics (shared state)
let ref_t = newTable[string, int]()
ref_t["key"] = 42
# Insert or update
t["key"] = 42
# Access (raises KeyError if missing)
let val = t["key"]
# Check if key exists
if t.hasKey("key"):
discard
# Get with default value
let val = t.getOrDefault("key", 0)
# Atomic check-and-set
if t.hasKeyOrPut("key", defaultValue):
# key already existed