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.

Print & read input

Print a value or a line

Rust

println! appends a newline. print! does not; explicit flushing can be needed before waiting for input. {:?} requests Debug formatting.

Fragment

Macros are available from the standard prelude. Custom types need Display or Debug as appropriate.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
println!("Hello");
println!("count={}", 3);
println!("{:?}", vec![1, 2]);

Write diagnostics to stderr

Rust

eprintln! writes a newline-terminated diagnostic to stderr.

Fragment

Return an appropriate Result/ExitCode from main when failure should affect the process status.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
eprintln!("Could not load settings");

Log a message with useful context

Rust

tracing records structured fields; a subscriber determines how they are emitted. These are optional dependencies, not std APIs.

Fragment

Add tracing and tracing-subscriber to Cargo.toml; initialize a global subscriber once in main, not per request.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
tracing_subscriber::fmt::init();
tracing::info!(item_id = 24, elapsed_ms = 8, "item saved");

Print or build text

Rust

println! adds a newline; print! does not. format! returns a String. {:?} uses Debug formatting, which quotes/escapes a string. to_owned() turns borrowed text into owned text.

Fragment

Fragment inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let name = "north";
let text = format!("item: {name}");
println!("{text:?}");

Recognize formatting controls

Rust

{} formats a value; {name} captures a local name. A colon introduces formatting options such as precision, alignment or zero padding. Formatting returns or prints text, depending on the macro.

Fragment

Independent fragments inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let price = format!("{:.1}", 6.2);
let tag = format!("{:>4}", "x");

Read a line

Rust

read_line appends to a String, retaining LF (and CR when present). Success returns the byte count; zero means EOF. ? propagates an I/O error. Clear a reused buffer when appropriate.

Fragment

Complete program; reads once.

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

fn main() -> io::Result<()> {
    let mut text = String::new();
    let bytes = io::stdin().read_line(&mut text)?;
    println!("{bytes}");
    Ok(())
}

Convert text to a number

Rust

parse::<T>() chooses the target type and returns Result. Integer parsing rejects surrounding whitespace and trailing junk; signed and unsigned target types accept different ranges.

Fragment

Independent fragment inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let result = "24".parse::<i32>();
match result {
    Ok(n) => println!("{n}"),
    Err(error) => eprintln!("{error}"),
}

Recognize raw input scaffolding

Rust

Read::read fills a byte buffer and returns a count; zero with a nonempty buffer signals EOF. from_utf8 validates bytes; expect panics on failure. A byte chunk can split UTF-8; arbitrary text requires retaining incomplete UTF-8 sequences across reads.

Fragment

Single-read demonstration; repeated reads need an explicit decoding/record boundary policy.

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

fn main() -> io::Result<()> {
    let mut input = io::stdin().lock();
    let mut buffer = [0_u8; 8];
    let n = input.read(&mut buffer)?;
    println!("{:?}", &buffer[..n]);
    Ok(())
}