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

C++

nlohmann/json is a named third-party implementation; dump produces JSON text from its value model.

Fragment

Install/include nlohmann/json.hpp. Standard C++23 has no built-in JSON codec.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <nlohmann/json.hpp>
#include <string>

nlohmann::json value = {{"name", "Ada"}, {"active", true}};
std::string text = value.dump();

Validate incoming JSON before using it

C++

Check the parsed value’s shape and field type before extracting it. Library exceptions and domain errors need an owner recovery policy.

Fragment

Requires nlohmann/json.hpp, <string> and <stdexcept>. Choose a separate whitespace policy if blank-but-nonempty names are invalid.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <nlohmann/json.hpp>
#include <stdexcept>
#include <string>

auto value = nlohmann::json::parse(text);
if (!value.is_object() || !value.contains("name") ||
    !value["name"].is_string()) {
    throw std::runtime_error("Expected a string name");
}
auto name = value["name"].get<std::string>();
if (name.empty()) throw std::runtime_error("name is required");

Inspect, split and slice text

C++

find… returns a position or npos. substr takes a start and count, not an end index. size counts char units. back/pop_back require a nonempty string; clear empties it, push_back appends a character.

Fragment

Header plus independent fragments.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <string>

std::string text = "north";
auto part = text.substr(1, 2);
auto pos = text.find_first_not_of(" ");
text.push_back('!');

Read text and character literals

C++

Single quotes form character literals; ordinary double-quoted literals are character arrays, not std::string objects. A raw literal preserves escapes. Since C++20, u8 literals use char8_t, so they are not ordinary char strings.

Fragment

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

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <string>

std::string name = "north";
auto path = R"(folder\file)";
char character = 'n';

Distinguish bytes, code points and characters

C++

std::string is a sequence of char units. For a UTF-8 payload, size counts bytes; it does not decode Unicode or count grapheme clusters. Ordinary indexing can select one byte of a multibyte sequence.

Fragment

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

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <string>

std::string text = "\xf0\x9f\xa6\x80";
auto bytes = text.size(); // 4 UTF-8 bytes

Append and replace text

C++

std::string owns mutable text. +=/append extend it; replace(position, count, text) changes a selected range. string_view does not own or grow storage and can be invalidated by changes to its source.

Fragment

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

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <string>

std::string text = "north";
text += " gate";
text.replace(0, 5, "west");

Parse structured JSON with a named implementation

C++

The standard library has no built-in JSON parser. A named dependency such as nlohmann/json defines its own parsing and error contract.

Fragment

Requires the nlohmann/json dependency and an app-provided source string; this is not a standard-library API.

main.cpp
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
#include <nlohmann/json.hpp>
auto value = nlohmann::json::parse(source);