Array Methods
Array<T> compiles to Rust's Vec<T>. All the standard JavaScript array methods work, compiling to Rust's iterator adaptors.
Creating arrays
const nums: Array<i32> = [1, 2, 3];
const empty: Array<string> = [];
const filled = new Array(5); // Vec with 5 default elements
const built = Array.of(1, 2, 3); // vec![1, 2, 3]
Adding and removing
const items: Array<string> = ["a", "b"];
items.push("c"); // Append to end
items.pop(); // Remove from end
items.unshift("z"); // Insert at start
items.shift(); // Remove from start
items.splice(1, 1); // Remove 1 element at index 1
Iterating
map
Transform each element:
const nums: Array<i32> = [1, 2, 3];
const doubled = nums.map(n => n * 2); // [2, 4, 6]
Generates: .iter().map(|n| n * 2).collect()
filter
Keep elements matching a predicate:
const nums: Array<i32> = [1, 2, 3, 4, 5];
const evens = nums.filter(n => n % 2 == 0); // [2, 4]
forEach
Side effects on each element:
const names: Array<string> = ["Alice", "Bob"];
names.forEach(name => console.log(name));
Reducing
const nums: Array<i32> = [1, 2, 3, 4];
const sum = nums.reduce((acc: i32, n: i32): i32 => acc + n, 0); // 10
reduceRight iterates from the end:
const reversed = nums.reduceRight((acc: Array<i32>, n: i32): Array<i32> => {
acc.push(n);
return acc;
}, []);
Searching
const nums: Array<i32> = [1, 2, 3, 4, 5];
nums.find(n => n > 3); // 4 (first match)
nums.findIndex(n => n > 3); // 3 (index of first match)
nums.findLast(n => n < 3); // 2 (last match)
nums.findLastIndex(n => n < 3); // 1 (index of last match)
nums.indexOf(3); // 2
nums.lastIndexOf(3); // 2
nums.includes(3); // true
Testing
const nums: Array<i32> = [1, 2, 3];
nums.some(n => n > 2); // true (at least one matches)
nums.every(n => n > 0); // true (all match)
Flattening
const nested: Array<Array<i32>> = [[1, 2], [3, 4]];
const flat = nested.flat(); // [1, 2, 3, 4]
const mapped = nested.flatMap(arr => arr); // [1, 2, 3, 4]
Slicing and concatenating
const arr: Array<i32> = [1, 2, 3, 4, 5];
const slice = arr.slice(1, 3); // [2, 3]
const combined = arr.concat([6, 7]); // [1, 2, 3, 4, 5, 6, 7]
In-place mutations
let arr: Array<i32> = [3, 1, 2];
arr.sort(); // [1, 2, 3]
arr.reverse(); // [3, 2, 1]
arr.fill(0); // [0, 0, 0]
Joining
const parts: Array<string> = ["hello", "world"];
const joined = parts.join(" "); // "hello world"
Iterators
const arr: Array<string> = ["a", "b", "c"];
arr.keys(); // 0, 1, 2
arr.values(); // "a", "b", "c"
arr.entries(); // [0, "a"], [1, "b"], [2, "c"]
Length
const arr: Array<i32> = [1, 2, 3];
const len = arr.length; // 3
Static methods
Array.isArray(arr); // true (always true, statically typed)
Array.from(iterable); // Collect iterator to Vec
Array.of(1, 2, 3); // vec![1, 2, 3]
Complete reference
| Method | Rust equivalent |
|--------|----------------|
| .push(item) | .push(item) |
| .pop() | .pop() |
| .shift() | .remove(0) |
| .unshift(item) | .insert(0, item) |
| .map(fn) | .iter().map(fn).collect() |
| .filter(fn) | .iter().filter(fn).collect() |
| .reduce(fn, init) | .iter().fold(init, fn) |
| .find(fn) | .iter().find(fn).cloned() |
| .forEach(fn) | .iter().for_each(fn) |
| .some(fn) | .iter().any(fn) |
| .every(fn) | .iter().all(fn) |
| .flat() | .iter().flatten().collect() |
| .flatMap(fn) | .iter().flat_map(fn).collect() |
| .indexOf(item) | .iter().position(\|x\| x == item) |
| .includes(item) | .contains(item) |
| .slice(s, e) | self[s..e].to_vec() |
| .join(sep) | .join(sep) |
| .sort() | .sort() |
| .reverse() | .reverse() |
| .length | .len() as i64 |