Chapter 10. Boston Commons

Never looked at you before with / Common sense

They Might Be Giants, “Circular Karate Chop” (2013)

In this chapter, you will write a Rust version of the comm (common) utility, which will read two files and report the lines of text that are common to both and the lines that are unique to each. These are set operations where the common lines are the intersection of the two files and the unique lines are the difference. If you are familiar with databases, you might also consider these as types of join operations.

You will learn how to:

  • Manually iterate the lines of a filehandle using Iterator::next

  • match on combinations of possibilities using a tuple

  • Use std::cmp::Ordering when comparing strings

How comm Works

To show you what will be expected of your program, I’ll start by reviewing part of the manual page for the BSD comm to see how the tool works:

COMM(1) BSD General Commands Manual COMM(1) NAME comm -- select or reject lines common to two files SYNOPSIS comm [-123i] file1 file2 DESCRIPTION The comm utility reads file1 and file2, which should be sorted lexically, and produces three text columns as output: lines only in file1; lines only in file2; and lines in both files. The filename ''-'' means the standard input. The following options are available: -1 Suppress printing of column 1. -2 Suppress printing of column 2. -3 Suppress printing of column 3. -i Case insensitive comparison of lines. Each column will have a number of tab characters ...

Get Command-Line Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.