Skip to content

Collections (List & Map)

Wysna provides high-performance, built-in collection primitives: list[T] (dynamic arrays) and dict[K, V] (hash maps). Both obey pure value semantics via Automatic Reference Counting (ARC) and Copy-on-Write (CoW).

A list is a contiguous, dynamically-sized array of elements of uniform type.

fn main() -> unit {
# Initializing a list of integers
let mut numbers = [10, 20, 30]
# Appending elements
numbers.append(40)
numbers.append(50)
# Length
print("Count:", len(numbers)) # 5
# Indexing
print("First:", numbers[0]) # 10
print("Last: ", numbers[len(numbers) - 1]) # 50
# Updating an element
numbers[1] = 99
print("Updated:", numbers) # [10, 99, 30, 40, 50]
}
  • list.append(item): Appends an element to the end of the list.
  • list.pop() -> T?: Removes and returns the last element, or none if empty.
  • len(list): Returns the current number of elements.

A dict is an associative hash map storing key-value pairs. Keys must belong to predictable types (int, str, bool, or enum); floating-point keys are prohibited compile-time (W2010).

fn main() -> unit {
let mut scores: dict[str, int] = {
"alice": 95,
"bob": 88,
}
# Inserting or updating a key
scores["charlie"] = 92
scores["alice"] = 100
# Safe retrieval with fallback via .get()
let bob_score = scores.get("bob") ?? 0
print("Bob score:", bob_score)
# Checking key existence via .get()
if scores.get("david") != none {
print("David is present")
} else {
print("David not found")
}
}

Assigning a dictionary creates a shared view until modified via Copy-on-Write:

fn main() -> unit {
let base = {"mode": "dark", "fontSize": "14"}
let mut custom = base
custom["fontSize"] = "16"
print("Base: ", base["fontSize"]) # 14
print("Custom:", custom["fontSize"]) # 16
}