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.

Libraries: tools & testing

The toolchain around the code: building and packaging, testing, formatting and linting, debugging and profiling, and documentation.

Toolchain: node/npm/tsc, the go command, cargo, compilers + CMake

Rust

rustup installs and updates the toolchain, and cargo is the build tool and package manager you use day to day. Cargo resolves dependencies from crates.io (Cargo.toml and Cargo.lock), calls the rustc compiler, runs tests and benchmarks, and runs rustfmt (cargo fmt) and Clippy (cargo clippy). You rarely call rustc directly.

Fragment

Terminal commands, run from the project folder.

terminal
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cargo new shop                      # project with Cargo.toml and src/main.rs
cargo add serde --features derive   # add a dependency
cargo run                           # build (debug) and run
cargo build --release               # optimized build in target/release
cargo test                          # unit, integration and doc tests
cargo fmt                           # format with rustfmt
cargo clippy                        # lints beyond compiler warnings
cargo doc --open                    # documentation for your crate and its dependencies

Most used: cargo new, cargo add, cargo run, cargo build --release, cargo test, cargo fmt, cargo clippy, rustup update.

A build.rs file at the package root is a build script: Cargo compiles and runs it before the package, for generating code, compiling bundled C sources (the cc crate) or telling Cargo what to link via cargo::rustc-link-lib lines.

Testing: node:test, testing, #[test], GoogleTest

Rust

Testing is built into the language and Cargo, so no library is needed. Mark functions with #[test], check with assert!, assert_eq! and assert_ne!, and keep unit tests in a #[cfg(test)] module beside the code, where they can test private functions. Files in tests/ are integration tests that use the public API. cargo test also runs code examples in doc comments.

Complete file

A library file with its unit tests.

src/lib.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
// src/lib.rs, run with: cargo test
pub fn total(prices: &[u32]) -> u32 {
    prices.iter().sum()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn adds_prices() {
        assert_eq!(total(&[40, 15]), 55);
    }

    #[test]
    #[should_panic(expected = "overflow")] // needs overflow checks: fails under cargo test --release
    fn overflow_panics_in_debug_builds() {
        total(&[u32::MAX, 1]);
    }
}

Most used: #[test], assert_eq!, assert!, #[should_panic(expected = "...")], #[cfg(test)] mod tests, Result-returning tests, cargo test name_filter.

Formatting & linting: Prettier/ESLint, gofmt/vet, rustfmt/Clippy, clang-format/tidy

Rust

rustfmt (cargo fmt) formats code and Clippy (cargo clippy) adds hundreds of lints for correctness, style and performance; both ship with rustup. Lints are configured in Cargo.toml's [lints] table, rustfmt.toml and clippy.toml, or with #[allow(...)] on items. cargo-audit and cargo-deny check dependencies for known vulnerabilities and licence problems.

Fragment

Terminal commands, run from the project folder.

terminal
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cargo fmt                     # format with rustfmt (cargo fmt --check in CI)
cargo clippy                  # hundreds of lints beyond compiler warnings
cargo clippy --fix            # apply suggested fixes
cargo clippy -- -D warnings   # fail on any warning (CI)
cargo audit                   # known vulnerabilities in Cargo.lock (cargo install cargo-audit)
cargo deny check              # licences, duplicates and advisories (cargo install cargo-deny)

Most used: cargo fmt, cargo fmt --check, cargo clippy, -D warnings, #[allow(clippy::...)], [lints] in Cargo.toml, cargo audit, cargo deny.

Debugging & profiling tools: inspector, Delve + pprof, LLDB + flamegraph, sanitizers

Rust

Rust uses the platform debuggers, LLDB and GDB, through the rust-lldb and rust-gdb wrappers or an IDE (VS Code with CodeLLDB, RustRover). RUST_BACKTRACE=1 prints panic backtraces, and dbg!(expr) prints a value with its file and line. For performance, criterion runs statistical benchmarks (on stable, cargo bench needs criterion or divan with harness = false; the built-in #[bench] is nightly-only), cargo-flamegraph and samply profile CPU time, and Miri checks unsafe code for undefined behaviour.

Fragment

Terminal commands, run from the project folder.

terminal
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
RUST_BACKTRACE=1 cargo run      # full backtrace when a panic happens
rust-lldb target/debug/app      # LLDB (or rust-gdb) with Rust pretty-printers
cargo bench                     # benchmarks; criterion is the common harness
cargo flamegraph --bin app      # CPU flame graph (cargo install flamegraph)
cargo +nightly miri test        # undefined behaviour in unsafe code

Most used: dbg!, RUST_BACKTRACE=1, rust-lldb, rust-gdb, CodeLLDB, criterion, cargo flamegraph, samply, cargo miri, tokio-console for async tasks.

Documentation tools: TSDoc/TypeDoc, go doc, rustdoc, Doxygen

Rust

Rust documentation is Markdown inside /// comments (for the next item) and //! comments (for the enclosing crate or module). cargo doc builds HTML documentation for your crate and all its dependencies, docs.rs publishes it for every crate on crates.io, and code blocks in doc comments run as tests with cargo test, so examples stay correct. Intra-doc links such as [`Vec`] link to other items.

Complete file

A library file in a crate named pricing; cargo test runs the example.

src/lib.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
//! Prices with tax. (`//!` documents the enclosing crate or module.)

/// Adds tax to a price in cents.
///
/// `rate` is a fraction, for example `0.2` for 20%. The result is rounded
/// to whole cents.
///
/// # Examples
///
/// ```
/// assert_eq!(pricing::with_tax(1000, 0.2), 1200);
/// ```
pub fn with_tax(cents: u64, rate: f64) -> u64 {
    (cents as f64 * (1.0 + rate)).round() as u64
}

Most used: ///, //!, # Examples, # Errors, # Panics, [`Item`] links, cargo doc --open, doc tests run by cargo test, #![warn(missing_docs)].