Skip to main content

Collections

Oxide provides type aliases for Rust's collection types and uses Rust's standard methods directly with automatic camelCase to snake_case conversion.

Array Type

Array<T> is a type alias for Vec<T>:

Creating Arrays

// Array literal
let numbers = [1, 2, 3, 4, 5]

// Empty array with type
var arr: Array<Int> = []

// Using Vec methods
var vec = Vec.new()
vec.push(1)

Array Methods

Arrays use Rust's Vec methods directly, plus some Oxide convenience methods:

MethodRust EquivalentDescription
isEmpty()is_empty()Checks if empty
len()len()Returns element count
first()first()First element reference
last()last()Last element reference
push(element)push(element)Adds to end
pop()pop()Removes and returns last
append(element)push(element)Alias for push (Oxide method)
removeLast()pop()Alias for pop (Oxide method)
insertAt(index, element)insert(index, element)Inserts at index (Oxide method)
removeAt(index)remove(index)Removes at index (Oxide method)
reversed()N/AReturns reversed copy (Oxide method)
sorted()N/AReturns sorted copy (Oxide method)
get(index)get(index)Safe index access
iter()iter()Iterator

Array Examples

var numbers = [3, 1, 4, 1, 5]

numbers.isEmpty() // false
numbers.len() // 5
numbers.first() // Some(&3)
numbers.last() // Some(&5)

// Non-mutating transforms (return new arrays)
let sorted = numbers.sorted() // [1, 1, 3, 4, 5]
let reversed = numbers.reversed() // [5, 1, 4, 1, 3]

// Mutating operations
numbers.append(9) // adds 9 to end
numbers.push(2) // also adds to end
let popped = numbers.removeLast() // removes and returns last

numbers.insertAt(1, 2) // inserts 2 at index 1
let removed = numbers.removeAt(0) // removes element at index 0

Dict Type

Dict<K, V> is a type alias for HashMap<K, V>:

Creating Dicts

// Dict literal
var scores = ["Alice": 100, "Bob": 85]

// Empty dict with type
var map: Dict<String, Int> = Dict.new()

// Using HashMap methods
var hashMap = HashMap.new()
hashMap.insert("key", "value")

Dict Methods

MethodRust EquivalentDescription
isEmpty()is_empty()Checks if empty
len()len()Returns entry count
containsKey(key)contains_key(key)Checks for key
get(key)get(key)Gets value reference
getMut(key)get_mut(key)Gets mutable reference
insert(key, value)insert(key, value)Inserts key-value
remove(key)remove(key)Removes entry
keys()keys()Key iterator
values()values()Value iterator
iter()iter()Entry iterator

Dict Examples

var scores = ["Alice": 100, "Bob": 85, "Carol": 92]

scores.isEmpty() // false
scores.len() // 3
scores.containsKey("Alice") // true
scores.get("Alice") // Some(&100)

// Modify
scores.insert("Dave", 78)
scores.remove("Bob") // Some(85)

// Iterate
for (name, score) in scores.iter() {
println!("$name: $score")
}

// Keys and values
for key in scores.keys() {
println!(key)
}
for value in scores.values() {
println!(value)
}

Iterator Methods

Iterators use Rust's standard methods plus some Oxide convenience aliases:

MethodRust EquivalentDescription
forEach(f)for_each(f)Calls closure on each
map(f)map(f)Transforms elements
filter(p)filter(p)Filters elements
flatMap(f)flat_map(f)Map and flatten
reduce(f)reduce(f)Reduce to one value
reduced(f)reduce(f)Alias for reduce (Oxide)
take(n)take(n)Take first n
taken(n)take(n)Alias for take (Oxide)
skip(n)skip(n)Skip first n
skipped(n)skip(n)Alias for skip (Oxide)
takeWhile(p)take_while(p)Take while true
skipWhile(p)skip_while(p)Skip while true
any(p)any(p)Any match?
all(p)all(p)All match?
find(p)find(p)Find first match
findFirst(p)find(p)Alias for find (Oxide)
count()count()Count elements
collect()collect()Collect into container
toArray()collect()Collect to Array (Oxide)
enumerate()enumerate()Add indices
enumerated()enumerate()Alias (Oxide)
zip(other)zip(other)Zip iterators
zipped(other)zip(other)Alias (Oxide)

Iterator Examples

let numbers = [1, 2, 3, 4, 5]

// Basic operations
numbers.iter().forEach { println!("$it") }

let doubled: [Int] = numbers.iter()
.map { it * 2 }
.toArray() // [2, 4, 6, 8, 10]

let evens: [Int] = numbers.iter()
.filter { it % 2 == 0 }
.toArray() // [2, 4]

// Reduce
let sum = numbers.iter()
.cloned()
.reduced { acc, next -> acc + next } // Some(15)

// Testing
numbers.iter().any { it > 3 } // true
numbers.iter().all { it > 0 } // true
numbers.iter().count() // 5

// Finding
let found = numbers.iter()
.findFirst { it > 3 } // Some(&4)

// Take and skip
numbers.iter().taken(3).toArray() // [&1, &2, &3]
numbers.iter().skipped(3).toArray() // [&4, &5]

// Enumerated
for (i, num) in numbers.iter().enumerated() {
println!("$i: $num")
}

// Zipped
let letters = ["a", "b", "c"]
for (num, letter) in numbers.iter().zipped(letters.iter()) {
println!("$num -> $letter")
}

Chaining Operations

let result = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
.iter()
.filter { it % 2 == 0 } // Even numbers
.map { it * it } // Square them
.takeWhile { it < 50 } // While less than 50
.toArray() // [4, 16, 36]

Comparison with Rust

OxideRust
arr.isEmpty()arr.is_empty()
arr.len()arr.len()
arr.append(x)arr.push(x)
arr.removeLast()arr.pop()
arr.insertAt(i, x)arr.insert(i, x)
arr.removeAt(i)arr.remove(i)
arr.sorted(){ let mut v = arr.clone(); v.sort(); v }
arr.reversed()arr.iter().rev().cloned().collect()
iter.map(f)iter.map(f)
iter.filter(p)iter.filter(p)
iter.reduced(f)iter.reduce(f)
iter.findFirst(p)iter.find(p)
iter.toArray()iter.collect::<Vec<_>>()
dict.containsKey(k)dict.contains_key(k)
dict.getMut(k)dict.get_mut(k)

See Also