Reference

Programming languages

Choose TypeScript, Go, Rust or C++ and find syntax, everyday APIs, standard libraries and tools in one place.

Scope and conventions

TypeScript/Node, Go 1.23+, Rust 2024 and C++23: from a first CLI to files, JSON, HTTP, concurrency and testing. Complete programs are labeled; other examples identify their surrounding code/imports. Optional libraries and version requirements are named in each entry.

Collections

Create an indexed collection

Rust

[T; N] is a fixed-size array; Vec<T> is growable. Indexing out of bounds panics. get returns Option<&T>; copied copies a Copy value from that borrow. pop returns Option<T>, so emptiness is explicit.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let fixed = [2, 5, 8];
let mut values: Vec<i32> = vec![2, 5];
values.push(8);

let first = values.get(0).copied();
let last = values.pop();

Look up a value by key

Rust

HashMap uses hashing; BTreeMap orders keys. get returns Option<&V>; copied makes an owned copy of a Copy value before later mutation. insert can replace an existing value; entry supports initialize-or-update operations.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
use std::collections::HashMap;

let mut scores = HashMap::new();
scores.insert("north", 24);

let score = scores.get("north").copied();
scores.remove("north");

Keep unique values

Rust

HashSet requires hashing/equality; BTreeSet uses ordering. insert returns whether the value was newly inserted. Collection equality rules come from the element type’s traits.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
use std::collections::HashSet;

let mut labels = HashSet::new();
labels.insert("north");

let found = labels.contains("north");
labels.remove("north");

Select a range or borrow a view

Rust

&values[a..b] borrows a slice and excludes b. &mut borrows for mutation; borrowing rules constrain other access while it is live. to_vec creates an owned vector by cloning the elements.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let values = vec![2, 5, 8];
let part: &[i32] = &values[1..3];
let separate = part.to_vec();

Sort with the right comparator shape

Rust

sort mutates the slice and is stable; sort_unstable does not preserve equal-element order. The comparator passed to sort_by returns Ordering, not bool. Floating-point values can use total_cmp when a total ordering is desired.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let mut values = vec![12, 2, 5];
values.sort();

values.sort_by(|a, b| b.cmp(a)); // descending

Map, filter and accumulate

Rust

iter borrows elements; into_iter on an owned collection consumes it, while iteration over &collection borrows its elements. Adapters are lazy until consumed by sum, collect, a loop, etc. copied turns borrowed Copy elements into values.

Fragment

Fragment: imports and type/function declarations at file scope; usage statements inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let values = vec![2, 5, 8];

let total: i32 = values.iter()
    .copied()
    .filter(|n| *n > 3)
    .map(|n| n * 2)
    .sum();