BurntSushi/ripgrep
Patterns and conventions
ripgrep's coding style is consistent across crates. New code should match.
Formatting
rustfmt.toml is short:
max_width = 79
use_small_heuristics = "Max"The 79-column line limit is unusual for modern Rust but enforced everywhere. cargo fmt is run in CI.
Edition and MSRV
edition = "2024"in every workspaceCargo.toml.rust-version = "1.85"for the binary; library crates may have older MSRVs declared in their ownCargo.toml.
Error handling
- Application code (the
rgbinary) usesanyhow::Result<T>for fallible functions and?for propagation. Seecrates/core/main.rs. - Library code (the
grep-*crates) defines its own error types using plainenums with#[derive(Debug)]and aDisplayimpl. Examples:grep_regex::Error(crates/regex/src/error.rs),globset::Error(crates/globset/src/lib.rs),ignore::Error(crates/ignore/src/lib.rs). - Library errors do not depend on
anyhoworthiserror. The convention is hand-rolled, partly to keep dependency counts low.
Logging
logfor crate-level diagnostics. Thergbinary configures it viacrates/core/logger.rs.err_message!andeprintln_locked!(defined incrates/core/messages.rs) are used for user-facing stderr messages. They acquire a global lock so concurrent worker threads do not interleave their output.
Documentation
#![deny(missing_docs)]is enabled at the top of every public library crate (crates/searcher/src/lib.rs,crates/printer/src/lib.rs,crates/regex/src/lib.rs,crates/matcher/src/lib.rs,crates/pcre2/src/lib.rs,crates/ignore/src/lib.rs). Public items must have///doc comments.crates/grepis a re-export facade and its lib.rs is intentionally tiny.- The CLI binary uses
/*! ... */module-level docs; flags carry their own doc strings via theFlagtrait (Flag::doc_shortandFlag::doc_long).
CLI flag definitions
Every flag is a type that implements trait Flag (defined in crates/core/flags/mod.rs). The Flag::update method receives a parsed value and updates LowArgs. The conventions:
- One flag = one type. Negations (
--no-foo) live on the same type viaFlag::name_negated. - Doc strings are static strings written in
mdocstyle. They feed the man page and--helpoutput. - The flag's category (
Category::Input,Search,Filter,Output,OutputModes,Logging,OtherBehaviors) controls where it appears inrg --helpand the man page. - The full list of flag definitions is
crates/core/flags/defs.rs(235K of mostly hand-written tables).
When adding a flag:
- Define a struct in
crates/core/flags/defs.rswithDebugand theFlagtrait impl. - Add the resolved field to
LowArgs(crates/core/flags/lowargs.rs). - If the flag affects searching, plumb it through
HiArgs(crates/core/flags/hiargs.rs). - Register the new flag in the
FLAGSslice at the bottom ofdefs.rs. - Add at least one integration test exercising both the flag and its negation.
Imports and modules
use std::{...}style preferred (grouped imports).- Crate names use kebab-case in Cargo (
grep-regex) and snake_case in code (grep_regex). - Module-internal helpers live in
pub(crate)-visible modules; only the items that should be externally usable arepub.
Performance
- The repository is performance-sensitive. Avoid allocations in hot paths.
- Hot search loops in
crates/searcher/src/searcher/{core,glue}.rsare written to compile down to tight code; do not refactor for "elegance" without benchmarks. - The
bstrcrate is used heavily because most byte slices are not guaranteed UTF-8. Preferbstrtraits overstr::from_utf8in performance-sensitive code.
Concurrency
- ripgrep does not use
tokioor any async runtime. crossbeamchannels andstd::threadprovide all parallelism.WalkParallel(crates/ignore/src/walk.rs) owns the thread pool; everything else is per-thread.- For shared mutable state, prefer
std::sync::atomicoverMutex. Existing examples:matched: AtomicBoolincrates/core/main.rs::search_parallel.
Naming
- File names are snake_case Rust source:
gitignore.rs,default_types.rs,line_buffer.rs. - Type names follow Rust conventions:
WalkBuilder,RegexMatcher,StandardSink. - Builders end in
Builderand produce aDefault::default()-initialised type viabuild(). - Internal modules sometimes use single words (
ast,ban,stripincrates/regex/src/). These are intentional; keep them short.
When in doubt
Read the surrounding code. Andrew Gallant's style is consistent across crates spanning a decade; the closest neighbour is almost certainly the right model.
Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.