About

Building CLI Tools with Rust

Practical command-line tools with clap + serde

Famous Rust CLI tools: ripgrep (grep replacement), fd (find replacement), bat (cat replacement), exa (ls replacement). There's a reason they're faster than originals.

Single binary distribution — no runtime installation like Python. Startup time in milliseconds means no perceived delay when called from shell scripts.

Basic Structure

use clap::Parser;

#[derive(Parser)]
struct Cli {
    #[arg(short, long)]
    input: String,
}

clap's derive macros make argument parsing a struct definition. serde handles JSON/TOML/YAML I/O in one line.

Key Points

1

Create project with cargo new mytool

2

Define CLI argument struct with clap derive

3

Serialize config/data with serde

4

Build optimized binary with cargo build --release

Pros

  • ✓ Single binary — deployment is one file
  • ✓ Startup ~1ms — fast even when called from shell scripts

Cons

  • ✗ Long compile times — minutes with many dependencies
  • ✗ Cross-compilation gets complex with C dependencies

Use Cases

ripgrep — search multi-GB codebases in seconds Data pipelines — JSON/CSV transformation CLI
← async/await — Asynchronous Programming in Rust Rust → WASM — Running Rust in the Browser →