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

C++

Stream insertion writes values. A newline character does not request the explicit flush that std::endl does.

Fragment

Include <iostream>; place calls inside a function.

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

std::cout << "Hello\n";
std::cout << "count=" << 3 << '\n';

Write diagnostics to stderr

C++

cerr is the standard error stream. Return a failure status from the owning main after resources have been released.

Fragment

Include <iostream>.

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

std::cerr << "Could not load settings\n";

Log a message with useful context

C++

clog is a standard diagnostic stream; it does not supply a standard structured logging schema or level filter.

Fragment

Include <iostream>. Choose a logging library explicitly when you need structured records, sinks or concurrency policies.

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

std::clog << "level=info item_id=24 elapsed_ms=8 saved\n";

Print or build text

C++

cout inserts into stdout; \n supplies the newline. ostringstream builds text; str() returns it. quoted escapes quotes/backslashes, not every control character. Application-specific quoting helpers can define a different format. C++20 std::format returns formatted text and C++23 std::println writes it with a newline, where the standard library supports them.

Fragment

Headers plus local fragments.

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

std::ostringstream out;
out << "item " << 24;
std::string text = out.str();

Recognize formatting controls

C++

fixed plus setprecision chooses decimal places. setw sets a minimum width for the next formatted field; setfill and fixed persist. imbue changes a stream’s locale. With C++20 std::format the same controls are format specs: std::format("{:.1f}", 6.2) and std::format("{:.>4}", "x").

Fragment

Headers plus independent formatting fragments.

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

std::ostringstream out;
out << std::fixed << std::setprecision(1) << 6.2;
out << std::setfill('.') << std::setw(4) << "x";

Read a line

C++

getline writes into the string and discards its delimiter (LF by default). With CRLF bytes, CR can remain. The returned stream is usable as a success condition.

Fragment

Complete program; reads once.

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

int main() {
    std::string text;
    if (std::getline(std::cin, text)) {
        std::cout << text.size();
    }
}

Convert text to a number

C++

from_chars reads a character range without skipping whitespace. ec reports errors; ptr marks where parsing stopped. Successful prefix parsing does not prove the whole range was consumed.

Fragment

Headers plus independent conversion fragment.

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

std::string text = "24";
int n = 0;
auto result = std::from_chars(
    text.data(), text.data() + text.size(), n);

Recognize raw input scaffolding

C++

read attempts a fixed-size read; gcount reports the actual count, including a final short read. Check that count as well as stream state. sizeof gives array size in bytes here; application-defined printer/helper names are not standard stream APIs.

Fragment

Complete single-read program; no line assembly.

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

int main() {
    char buffer[8];
    std::cin.read(buffer, sizeof buffer);
    const auto n = std::cin.gcount();
    std::cout << n;
}