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.

Files & paths

Join paths using the platform’s path API

Rust

Path/PathBuf represent native paths, including strings that may not be Unicode.

Fragment

An absolute joined component can replace an earlier path; establish a containment policy explicitly when needed.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let file = std::path::Path::new(directory).join("settings.json");

List the immediate entries in a directory

Rust

read_dir and each yielded entry can fail. Ordering is unspecified; collect and sort when a stable order matters.

Fragment

The enclosing function returns a compatible Result.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
for entry in std::fs::read_dir(directory)? {
    let entry = entry?;
    println!("{:?}", entry.path());
}

Create a directory and missing parents

Rust

create_dir_all creates missing parents. It can fail after creating some of the requested path.

Fragment

The enclosing function returns a compatible Result; the operation is not an atomic rollback transaction.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
std::fs::create_dir_all(directory)?;

Copy a chosen file with an overwrite policy

Rust

fs::copy copies file contents and relevant permissions, replacing an existing target. Choose another protocol if replacement is forbidden.

Fragment

The surrounding function returns a compatible Result. Metadata/link behavior follows the platform and API contract.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let copied_bytes = std::fs::copy(source, destination)?;

Read file text with explicit error and encoding behavior

Rust

read_to_string reads the complete file and fails on invalid UTF-8. read returns raw bytes instead.

Fragment

Place ? in a compatible Result-returning function.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let text = std::fs::read_to_string("notes.txt")?;
let bytes = std::fs::read("notes.txt")?;

Write or replace file contents

Rust

fs::write creates or replaces a file with the provided bytes and returns an I/O Result.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
std::fs::write("output.txt", b"ready\n")?;