Core Concepts / Collections & Algorithms

Collections & Algorithms

Vec Dynamic Array

let v = Vec::new();
v.push(1);
v.push(2);
print(v.len());      // 2
print(v.get(0));     // 1
v.set(1, 9);
print(v.pop());      // 9
Vec::new() / Vec::with_cap(n)create new / pre-allocate
v.push(x) / v.pop()append to the end / pop from the end (returns 0 when empty)
v.len() / v.is_empty()length / is empty
v.get(i)read the i-th element (returns 0 when out of bounds)
v.set(i, x)write the i-th element (ignored when out of bounds)

Native Vec has no bounds checking: out-of-bounds access is undefined behavior (may segfault), so you must ensure indexes are valid yourself.

HashMap / HashSet (i64 keys)

let m = hash_map_new<i64>();
m.insert(1, 100);
print(m.get(1, -1));    // 100
m.remove(1);

Open addressing + linear probing; when the load factor exceeds 1/2 it auto-grows by doubling. Keys can only be i64.

BTreeMap / BTreeSet (Ordered)

let b = btree_map_new();
b.insert(3, "c");
b.insert(1, "a");
print(b.get(1, ""));   // "a"

Ordered map: parallel sorted arrays + binary search (O(log n) lookups, O(n) inserts).

LinkedList

Doubly linked list: push_front / push_back / pop_front / pop_back / get(i, def) / set(i, v) / remove(i) / reverse / clear. Endpoint operations are O(1).

Algorithms

let v = Vec::new();
v.push(3); v.push(1); v.push(2);
sort(v);                    // 原地快排
print(binary_search(v, 2)); // 1
reverse(v);
  • sort(v) in-place quicksort
  • binary_search(v, target) returns the index or -1
  • reverse(v) reverses in place
  • Paths & JSON (Standard Library)

  • Paths: path_join / path_basename / path_dirname / path_extension (consistent across platforms, separator /)
  • JSON encode: json_escape / json_string / json_number_i64 / json_number_f64 / json_bool / json_null / json_join
  • JSON decode: json_parse_i64 / json_parse_bool / json_unescape / json_find_key
  • Aero 文档 · 基于 Aero 1.2.0 文档整理 · MIT
    SereinTeam