MARY.WIN

RUSTY — DESIGN PATTERNS — 01

Composition root:
where ripgrep decides what it is made of

Pattern 1 of 12 · In the life of one value: born.
Part of Rust design patterns.

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.

Where ripgrep's search worker gets its parts: two callers in main.rs, three constructors in hiargs.rs, one receiver in search.rs 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:

TERMINAL
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

  1. Without looking back: which argument differs between the two search_worker calls?
  2. After Example 1's statement, can search still 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.

The sources.

Each article names its sources in brackets, with the file, chapter or line to look at. Line numbers are for the tag or commit each article names and will move. Each article says which toolchain its tests passed on: rustc 1.100.0 for eleven of them, rustc 1.97.0 on Linux for Adapter.

SOURCES — 3
  • [S4] BurntSushi/ripgrep at commit 3e85c6c148fdce38a99ceeb9d2780dc7b03b4a4f: crates/core/main.rs, crates/core/flags/hiargs.rs, crates/core/search.rs, read 2026-11-29.
  • [S9] Mark Seemann, "Composition Root", blog.ploeh.dk, 28 July 2011, https://blog.ploeh.dk/2011/07/28/CompositionRoot/, read 2026-11-29.
  • PHP manual, "Objects and references", https://www.php.net/manual/en/language.oop5.references.php, read 2026-11-29: an object variable holds an identifier, and passing or assigning it copies the identifier, which points to the same object.
NOT CHECKED — 2
  • The statement that neither of the two books this series leans on uses the term "composition root" is a claim about two whole books, and the books were not searched again for this page.
  • The Testing section is a text search of the pinned source, not a test. ripgrep's own test suite was not run.