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.

Strings & text

Serialize a value as JSON

Rust

serde_json encodes supported Serde values. It is an optional crate, and serializing your own type can require a Serialize derive.

Fragment

Add serde_json to Cargo.toml. The surrounding function returns a compatible Result.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let value = serde_json::json!({ "name": "Ada", "active": true });
let json = serde_json::to_string(&value)?;

Validate incoming JSON before using it

Rust

Serde can require named fields and reject unknown fields. Parsing success still needs domain validation such as nonempty names.

Fragment

Add serde with derive and serde_json. Example assumes a Result with a compatible boxed error.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields)]
struct Input { name: String }
let input: Input = serde_json::from_str(text)?;
if input.name.trim().is_empty() {
    return Err("name is required".into());
}

Inspect, split and slice text

Rust

trim borrows a trimmed view. split_whitespace yields an iterator; next returns Option. len counts bytes; chars visits Unicode scalar values. String supports push, pop and clear; &str does not mutate owned text.

Fragment

Independent fragments inside a function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let mut words = "north west".split_whitespace();
let first = words.next();
let clean = " west ".trim();
let empty = clean.is_empty();

Read text and character literals

Rust

Double quotes produce borrowed string literals; single quotes produce char values. Raw string literals preserve backslashes. String::from or to_owned creates owned, growable text.

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 name: &str = "north";
let path = r#"folder\file"#;
let character: char = 'n';
let owned = name.to_owned();

Distinguish bytes, code points and characters

Rust

str/String contain valid UTF-8; len counts bytes and chars visits Unicode scalar values. Integer character indexing is not supported. Byte slicing must land on UTF-8 boundaries; scalar values are not grapheme clusters.

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 text = "🦀";
let bytes = text.len(); // 4
let scalars = text.chars().count(); // 1

Append and replace text

Rust

String owns mutable UTF-8 text; push adds one char, push_str adds borrowed text. replace returns a new String rather than modifying the original. &str is a borrowed view, not a growable buffer.

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 text = String::from("north");
text.push(' ');
text.push_str("gate");

let changed = text.replace("north", "west");

Parse structured JSON with a named implementation

Rust

serde_json is an optional crate, not part of std. It can parse a generic JSON value or a serde-derived application type.

Fragment

Declare serde_json in Cargo.toml. serde derive requires its appropriate feature for typed models.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let value: serde_json::Value = serde_json::from_str(source)?;