The list module provides a dynamically sized array type called List. It accepts a single template parameter for the element type and relies on the currently configured memory allocator for storage. The List implementation is also clever enough to distinguish between Conjure’s native string type and other types to provide optimized storage for strings.
Lists are dynamically sized arrays that can grow and shrink at runtime. They’re part of the standard library.
var numbers = List[i32]{}
numbers.append(10)
numbers.append(20)
numbers.append(30)
c.printf("Length: %d\n", numbers.len) // 3Lists manage their own memory and automatically reallocate when needed.
List Operations
Lists support common operations like appending, inserting, and removing elements.
var items = List[string]{}
items.append("first")
items.append("second")
items.insert(1, "middle") // insert at index 1
items.remove(0) // remove element at index 0
var last = items.pop() // remove and return last elementAccess list elements using the same bracket syntax as arrays.
var value = items[0]
items[1] = "updated"List Capacity
Lists grow automatically, but you can pre-allocate capacity for better performance.
var numbers = List[i32].withCapacity(100)
for i in 0..100 {
numbers.append(i) // no reallocation needed
}This avoids multiple reallocations when you know approximately how many elements you’ll need.
Converting Between Types
Convert arrays to slices by taking a slice of the entire array.
var arr = [5]i32{1, 2, 3, 4, 5}
var slice []i32 = arr[..]Convert slices to lists by copying.
var slice = []i32{1, 2, 3, 4, 5}
var list = List[i32].from(slice)Convert lists to slices using the .slice() method.
var list = List[i32]{}
list.append(1)
list.append(2)
var slice = list.slice()