rust
19 lines · 1 tab
Marcus Chen
Jan 2026
1 tab
use clap::Parser;
#[derive(Parser, Debug)]
#[command(author, version, about)]
struct Args {
#[arg(short, long)]
input: String,
#[arg(short, long, default_value = "output.txt")]
output: String,
#[arg(short, long)]
verbose: bool,
}
fn main() {
let args = Args::parse();
println!("{:?}", args);
}
1 file · rust
Explain with highlit
For CLI tools, clap is the de facto standard. Version 4+ supports derive macros, letting you define arguments as a struct with attributes. The library auto-generates help text, validates inputs, and supports subcommands. I annotate fields with #[arg(short, long)] for flags, #[command(subcommand)] for nested commands, and value_parser for type checking. Clap handles --help, --version, and error messages automatically. The resulting code is declarative and self-documenting. For complex CLIs with multiple levels (like git or cargo), clap's subcommand support scales well. The compile-time safety means typos in argument names are caught early, and the generated help is always in sync with the code.
Related snips
rust
struct Config<'a> {
name: &'a str,
value: &'a str,
}
fn parse_config(line: &str) -> Config {
Lifetime annotations for flexible borrowing in structs
rust
lifetimes
borrowing
by Marcus Chen
1 tab
rust
use crossbeam::channel::unbounded;
use std::thread;
fn main() {
let (tx, rx) = unbounded();
Crossbeam for advanced concurrent data structures
rust
concurrency
lock-free
by Marcus Chen
1 tab
rust
use tracing::{info, instrument};
#[instrument]
fn process_request(user_id: u64) {
info!(user_id, "Processing request");
// Work happens here
tracing for structured logging and distributed tracing
rust
observability
tracing
by Marcus Chen
1 tab
rust
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
Channels (mpsc) for message passing between threads
rust
concurrency
channels
by Marcus Chen
1 tab
rust
macro_rules! create_function {
($func_name:ident) => {
fn $func_name() {
println!("Called {}", stringify!($func_name));
}
};
Declarative macros (macro_rules!) for code generation
rust
macros
metaprogramming
by Marcus Chen
1 tab
rust
use my_crate::add;
#[test]
fn test_public_api() {
assert_eq!(add(3, 4), 7);
}
Integration tests in tests/ directory
rust
testing
integration
by Marcus Chen
1 tab
Share this code
Here's the card — post it anywhere.