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.

Start, build & configure

Start a project and run your first app

Rust

Cargo creates a binary package and manages its dependencies/builds. Replace src/main.rs with the next complete example.

Fragment

Requires rustup/Rust and Cargo. -- separates Cargo options from your application arguments.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cargo new hello-cli
cd hello-cli
# Replace src/main.rs with the CLI example, then:
cargo run -- Ada
cargo build --release

A complete small CLI app

Rust

main is the binary entry point. args supplies Unicode arguments; use args_os for arbitrary native OS strings.

Complete program

Save as src/main.rs in a Cargo binary project. cargo run -- Ada prints the greeting.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
fn main() {
    let name = std::env::args().nth(1)
        .unwrap_or_else(|| "world".to_owned());
    println!("Hello, {name}!");
}

Read and validate an environment setting

Rust

var distinguishes absence and non-Unicode environment data. Parse the actual value rather than silently substituting a default for every error.

Fragment

The enclosing function returns Result<_, Box<dyn std::error::Error>>. Port 0 is deliberately excluded here; it has a separate OS-assigned-port meaning.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let raw = match std::env::var("PORT") {
    Ok(value) => value,
    Err(std::env::VarError::NotPresent) => "3000".to_owned(),
    Err(error) => return Err(error.into()),
};
let port: u16 = raw.parse()?;
if port == 0 { return Err("PORT must be 1..65535".into()); }

Parse named command-line options

Rust

The optional clap crate can derive a typed parser with help and validation.

Fragment

Add clap with derive. Put the type declaration at module scope and Args::parse inside main; try --name Ada -v.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
use clap::Parser;
#[derive(Parser)]
struct Args {
    #[arg(long, default_value = "world")]
    name: String,
    #[arg(short, long)]
    verbose: bool,
}
let args = Args::parse();

Start another program with an argument list

Rust

Command accepts separate arguments and returns status/stdout/stderr. Success in spawning/waiting is distinct from a successful exit status.

Fragment

The enclosing function returns a compatible Result; git must be installed. std::process::Command does not provide a built-in timeout. output buffers both streams.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
let output = std::process::Command::new("git")
    .arg("--version").output()?;
if !output.status.success() { return Err("git failed".into()); }
print!("{}", String::from_utf8_lossy(&output.stdout));

Recognize source, build and output

Rust

cargo build uses the development profile; --release selects a separate profile/output. cargo run builds and runs the chosen target. Cargo.toml names the package; executable names may be configured separately.

Fragment

Shell commands; Cargo binary package named app with src/main.rs. Pilot binary: preview.

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
cargo build
./target/debug/app

# Build and run current target:
cargo run

Read command-line arguments

Rust

args_os retains native OS strings, including arguments that cannot be represented as UTF-8. args instead requires valid Unicode.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
for argument in std::env::args_os().skip(1) {
    println!("{:?}", argument);
}

Distinguish an unset environment variable from an empty one

Rust

var distinguishes absence from invalid Unicode through VarError. var_os preserves native OS strings.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
match std::env::var_os("APP_MODE") {
    Some(value) => println!("{:?}", value),
    None => println!("unset"),
}

Return a process status without skipping required cleanup

Rust

main can return Result or ExitCode. process::exit does not run ordinary stack destructors.

Fragment

src/main.rs
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
fn main() -> std::process::ExitCode {
    std::process::ExitCode::FAILURE
}