Learning question: When you open a Rust program you did not write, where do you look to find out which concrete parts it runs on?
A program's behaviour depends on which matcher, which searcher and which output it was assembled from, and somebody has to choose. This article looks at one place where that choice is made in ripgrep: who constructs each part, who receives it, and what the receiving type is allowed to know.
Scope. We read three files of ripgrep at commit 3e85c6c [S4]. We do not cover dependency-injection containers, and we do not cover how ripgrep parses its flags. You need structs, functions and Result. You do not need lifetimes; the one generic parameter, W, is explained where it appears. Composition root is Mark Seemann's name for the preferably single place, as close as possible to a program's entry point, where its parts are composed [S9]. Neither of the two books this series leans on uses the term, so the mapping to ripgrep below is my reading and is marked as such.
A worker that opens its own output
The first plan for a search worker is usually that it looks after itself. It would build its regex matcher from the pattern, build its searcher, and open standard output, the way a houseguest who brings his own towels expects to be no trouble. It would then need nothing from anyone, and its constructor would take no arguments at all. The second caller wanted to run one worker on each thread, and there was one standard output.
Predict
Before reading on: ripgrep has a sequential search and a parallel search. If the worker type is the same in both, which of its three parts has to differ between them, and where would you expect that difference to be written down?
Name the parts
| Role | In ripgrep | Where |
|---|---|---|
| Composing site | search and search_parallel |
crates/core/main.rs lines 107 and 164 |
| Constructing site | HiArgs::matcher, HiArgs::searcher, HiArgs::printer |
crates/core/flags/hiargs.rs lines 386, 732, 580 |
| Receiver | SearchWorker<W> |
crates/core/search.rs line 230 |
Who builds the parts?
Handing every worker the job of constructing its own collaborators means each new way of running the program edits the worker. Thankfully, there's a smaller arrangement: construct the parts in one place and pass them in. Take a look at Example 1, the start of ripgrep's sequential search.
Example 1. The composing site (crates/core/main.rs, lines 119 to 123 [S4])
let mut searcher = args.search_worker(
args.matcher()?,
args.searcher()?,
args.printer(mode, args.stdout()),
)?;
There are five calls here, counting args.stdout() inside the printer's arguments, and one of them matters most. args.matcher(), args.searcher() and args.printer(..) each construct a finished value. args.search_worker(..) receives all three by value, which in Rust means they are moved: after this statement, the worker owns them and nothing else in search can use them.¹ The ? after three of the calls returns early if construction failed, so a worker never exists with a part missing.
Now take a look at Example 2, the type on the receiving end.
Example 2. The receiver (crates/core/search.rs, lines 230 to 241, comments removed [S4])
pub(crate) struct SearchWorker<W> {
config: Config,
command_builder: grep::cli::CommandReaderBuilder,
decomp_builder: Option<grep::cli::DecompressionReaderBuilder>,
matcher: PatternMatcher,
searcher: grep::searcher::Searcher,
printer: Printer<W>,
}
The last three fields are the three arguments from Example 1. W is a type parameter: it stands for whatever the printer writes to, and SearchWorker is compiled separately for each concrete W it is used with. The worker's code never names standard output.
Here is the answer to the prediction.
search_parallel makes the same call at lines 174 to 178, and one argument differs: args.printer(mode, bufwtr.buffer()) where the sequential path has args.printer(mode, args.stdout()). The difference is written at the composing site, on one line, and search.rs did not change to allow it.
What transfers, and what does not
If you write PHP, constructor injection is the same idea: a class receives its collaborators and does not new them. What does not transfer is the picture of a container resolving them at run time. Here the "container" is a function body you can read top to bottom, and the choice of W is fixed when the program is compiled. Passing a value in also hands over ownership, which a PHP object handle never does.
Is this one root or two?
So the parts are passed in, and the article could end here. But look at the table again: construction lives in hiargs.rs and composition in main.rs, and HiArgs::search_worker (line 715) sits between them and sets five more options on a builder before it calls build. Whether you count that as one composition root spread over two files, or as a root plus a factory, depends on what you want the name to do for you, and Seemann's post describes composing object graphs and names the composition root as the only appropriate place to use a DI container [S9]. If the name is going to have a place in your own code review vocabulary, which would you rather it meant: the file where types are chosen, or the function where they meet?
Practice tier: judgment. No compiler rule or lint asks for a composition root. You get one by deciding to.
Testing
The claim is that SearchWorker constructs none of its three collaborators. A check that would fail if the claim were false is a search of search.rs for their builders:
grep -n 'RegexMatcherBuilder\|SearcherBuilder::new\|StandardBuilder\|std::io::stdout' crates/core/search.rs
grep -n 'RegexMatcherBuilder::new\|SearcherBuilder::new\|StandardBuilder::new' crates/core/flags/hiargs.rs
432: let mut builder = grep::pcre2::RegexMatcherBuilder::new();
480: let mut builder = grep::regex::RegexMatcherBuilder::new();
631: let mut builder = grep::printer::StandardBuilder::new();
740: let mut builder = grep::searcher::SearcherBuilder::new();The first search prints nothing; the second finds all of them in hiargs.rs. This is a text check on the pinned source, run on 2026-11-29. ripgrep's own test suite was not run for this article.
Retrieve
- Without looking back: which argument differs between the two
search_workercalls? - After Example 1's statement, can
searchstill use the printer it built? Why not?
We come back to "passed in, and owned" in the Strategy article, where the thing passed in is a type and not a value.
TL;DR
ripgrep constructs its matcher, searcher and printer in flags/hiargs.rs, hands them to SearchWorker from two call sites in main.rs, and the worker type constructs none of them. The sequential and parallel searches differ in one argument at the call site. Passing the parts in moves them, so the worker is their only owner.
¹ "Moved" is the language's word for the old name losing the value. It says nothing about bytes being copied.