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.

Functions

Define and call a function

Rust

-> introduces the return type. The final expression without a semicolon is returned; return can exit early. () is the unit type.

Fragment

Function declaration, then a call fragment inside main.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
fn double(n: i32) -> i32 {
    n * 2
}

let result = double(6);

Find the entry point

Rust

main is the entry point. It can return (), or a compatible Result: Ok(()) represents successful completion in the I/O examples.

Fragment

Complete main.rs.

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

fn main() -> io::Result<()> {
    println!("north");
    Ok(())
}

Recognize custom types and methods

Rust

struct holds fields; impl holds methods. &mut self borrows the instance for mutation. derive(Default) generates a default value when fields support it. LinePrinter is exercise-defined.

Fragment

Custom declarations; use inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#[derive(Default)]
struct Counter {
    value: i32,
}

impl Counter {
    fn add(&mut self) {
        self.value += 1;
    }
}

let mut c = Counter::default();
c.add();

Construct a custom value

Rust

An associated function without a self parameter is called through the type, such as Token::new(24). new is a naming convention, not a keyword. Self names the implementing type; Self { value } constructs its fields using shorthand.

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+
struct Token {
    value: i32,
}

impl Token {
    fn new(value: i32) -> Self {
        Self { value }
    }
}

let item = Token::new(24);

Pass a function or capture a value

Rust

Closures infer parameter/capture behavior from use. Fn, FnMut and FnOnce describe how they can be called. move captures by value; capturing a reference by value still holds a reference, not a clone of its referent.

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 factor = 3;
let scale = |n: i32| n * factor;

let result = scale(2);

Recognize defaults and variable arguments

Rust

Ordinary Rust functions have no default arguments or general variadic parameter list. Slices, Option parameters, builders or macros can express the intended API. This example accepts a variable-length slice.

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+
fn sum(values: &[i32]) -> i32 {
    values.iter().sum()
}

let total = sum(&[2, 5]);