Search & Regex
Fast search keeps Rust codebases navigable. ripgrep (rg) honors .gitignore, fd finds paths, and sed/awk wrangle build logs and CSV exports.
Recipe
rg "AppError" src/
rg -t rust "unwrap\(" --glob '!tests/**'
fd '\.rs$' src/
rg -l "sqlx::query" | xargs wc -lWhen to reach for this:
- Finding all uses before refactor
- Auditing
unwraporunsafein production paths - Locating stale TODOs before release
Working Example
# Find enum variants and impl blocks
rg "enum AppError" -A 20 src/
# Files importing axum but not in router module
rg -l "use axum" src/ | rg -v routes
# Count lines per module
fd . src -e rs | xargs wc -l | sort -n
# Transform CI log excerpt
sed -n '/error\[E/,+3p' ci.logWhat this demonstrates:
-t rustfilters file types--globexcludes test trees- Pipelines combine
rg,fd, andwc
Deep Dive
ripgrep Flags
| Flag | Use |
|---|---|
-F | Fixed string (no regex) |
-w | Whole word |
-C 3 | Context lines |
--stats | Match counts and timing |
-z | Search in .tar etc. |
vs IDE search
CLI search is scriptable in CI (e.g. fail if todo!() in src/ outside tests).
Gotchas
grep -rincludes target/ - slow and noisy. Fix: usergor--exclude-dir=target.- Regex special chars -
.matches any char. Fix:rg -Ffor literal strings. - Case sensitivity - missed matches. Fix:
-iwhen appropriate. - Huge binary false positives - rare in Rust repos. Fix:
rgskips binaries by default. - sed in-place on macOS - different syntax. Fix: document GNU sed or use
perl -pi.
Alternatives
| Alternative | Use When | Don't Use When |
|---|---|---|
git grep | Only tracked files | Need ignore-aware speed |
ast-grep | Structural Rust search | Simple text lookup |
| rust-analyzer symbol search | Type-aware nav | CI scripting |
FAQs
rg vs ag?
rg is faster and more maintained; default for Rust teams.
Search workspace members?
Run from workspace root; rg searches all crates unless scoped with -g.
Related
- regex - in-Rust regex
- Rust-based CLI tools
- Pipes & text processing
- Codebase orientation
Stack versions: This page was written for Rust 1.97.0 (edition 2024), Tokio 1.x, Axum 0.8, serde 1.0, sqlx 0.8, clap 4, and Polars 0.46+.