πŸ’»

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