Chapter 6. Den of Uniquity
There’s only one everything
They Might Be Giants, “One Everything” (2008)
In this chapter, you will write a Rust version of the uniq
program (pronounced unique), which will find the distinct lines of text from either a file or STDIN
.
Among its many uses, it is often employed to count how many times each unique string is found.
Along the way, you will learn how to do the following:
-
Write to a file or
STDOUT
-
Use a closure to capture a variable
-
Apply the don’t repeat yourself (DRY) concept
-
Use the
Write
trait and thewrite!
andwriteln!
macros -
Use temporary files
-
Indicate the lifetime of a variable
How uniq Works
As usual, I’ll start by explaining how uniq
works so that you understand what is expected of your program.
Following is part of the manual page for the BSD version of uniq
.
The challenge program in this chapter will only implement the reading of a file or STDIN
, writing to a file or STDOUT
, and counting the lines for the -c
flag, but I include more of the documentation so that you can see the full scope of the program:
UNIQ(1) BSD General Commands Manual UNIQ(1) NAME uniq -- report or filter out repeated lines in a file SYNOPSIS uniq [-c | -d | -u] [-i] [-f num] [-s chars] [input_file [output_file]] DESCRIPTION The uniq utility reads the specified input_file comparing adjacent lines, and writes a copy of each unique input line to the output_file. If input_file is a single dash ('-') or absent, the standard input is read. If ...
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.