Skip to content
ImagineWorld

Den of Uniquity

node_idnode-book_command-line-rust_chapter-06-den-of-uniquity
node_typechapter-node
schema_profileworld.chapter_node.v1
statusactive
entropyhigh
manifoldworld
domainimagineworld.net
authorityinforms
layercorpus
chapter_num6
chapter_titleDen of Uniquity
governed_byCORPUS
constitutional_citationCONSTITUTION
local graph

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 the write! and writeln! 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 output_file is absent, standard output is used for output. The second and succeeding copies of identical adjacent input lines are not written. Repeated lines in the input will not be detected if they are not adja- cent, so it may be necessary to sort the files first.

The following options are available:

-c Precede each output line with the count of the number of times the line occurred in the input, followed by a single space.

-d Only output lines that are repeated in the input.

-f num Ignore the first num fields in each input line when doing compar- isons. A field is a string of non-blank characters separated from adjacent fields by blanks. Field numbers are one based, i.e., the first field is field one.

-s chars Ignore the first chars characters in each input line when doing comparisons. If specified in conjunction with the -f option, the first chars characters after the first num fields will be ignored. Character numbers are one based, i.e., the first char- acter is character one.

-u Only output lines that are not repeated in the input.

-i Case insensitive comparison of lines.

In the 06_uniqr/tests/inputs directory of the book’s Git repository, you will find the following input files I’ll use for testing:

  • empty.txt: an empty file
  • one.txt: a file with one line of text
  • two.txt: a file with two lines of the same text
  • three.txt: a file with 13 lines of 4 unique values
  • skip.txt: a file with four lines of two unique values plus an empty line

The other files t[1-6].txt are examples from a Perl program (https://oreil.ly/I9QA5) used to test the GNU version. These are generated by the mk-outs.sh file:

$ cat mk-outs.sh
#!/usr/bin/env bash
ROOT="tests/inputs"
OUT_DIR="tests/expected"
[[ ! -d "$OUT_DIR" ]] && mkdir -p "$OUT_DIR"
# Cf https://github.com/coreutils/coreutils/blob/master/tests/misc/uniq.pl
echo -ne "a\na\n" > $ROOT/t1.txt ①
echo -ne "a\na" > $ROOT/t2.txt ②
echo -ne "a\nb" > $ROOT/t3.txt ③
echo -ne "a\na\nb" > $ROOT/t4.txt ④
echo -ne "b\na\na\n" > $ROOT/t5.txt ⑤
echo -ne "a\nb\nnc\n" > $ROOT/t6.txt ⑥
for FILE in $ROOT/*.txt; do
BASENAME=${$(basename "$FILE")}
uniq $FILE > ${OUT_DIR}/${BASENAME}.out
uniq -c $FILE > ${OUT_DIR}/${BASENAME}.c.out
uniq < $FILE > ${OUT_DIR}/${BASENAME}.stdin.out
uniq -c < $FILE > ${OUT_DIR}/${BASENAME}.stdin.c.out
done

① Two lines each ending with a newline

② No trailing newline on last line

③ Two different lines, no trailing newline

④ Two lines the same; last is different with no trailing newline

⑤ Two different values with newlines on each

⑥ Three different values with newlines on each

To demonstrate uniq, note that it will print nothing when given an empty file:

Terminal window
$ uniq tests/inputs/empty.txt

Given a file with just one line, the one line will be printed:

Terminal window
$ uniq tests/inputs/one.txt
a

It will also print the number of times a line occurs before the line when run with the -c option. The count is right-justified in a field four characters wide and is followed by a single space and then the line of text: $ uniq -c tests/inputs/one.txt 1 a

The file tests/inputs/two.txt contains two duplicate lines:

$ cat tests/inputs/two.txt a a

Given this input, uniq will emit one line:

$ uniq tests/inputs/two.txt a

With the -c option, uniq will also include the count of unique lines:

$ uniq -c tests/inputs/two.txt 2 a

A longer input file shows that uniq only considers the lines in order and not globally. For example, the value a appears four times in this input file:

$ cat tests/inputs/three.txt a a b b a c c c a d d d d

When counting, uniq starts over at 1 each time it sees a new string. Since a occurs in three different places in the input file, it will also appear three times in the output:

$ uniq -c tests/inputs/three.txt 2 a 2 b 1 a 3 c 1 a 4 d

If you want the actual unique values, you must first sort the input, which can be done with the aptly named sort command. In the following output, you’ll finally see that a occurs a total of four times in the input file:

$ sort tests/inputs/three.txt | uniq -c 4 a 2 b 3 c 4 d

The file tests/inputs/skip.txt contains a blank line:

$ cat tests/inputs/skip.txt a

a b

The blank line acts just like any other value, and so it will reset the counter:

$ uniq -c tests/inputs/skip.txt 1 a 1 a 1 b

If you study the Synopsis of the usage closely, you’ll see a very subtle indication of how to write the output to a file. Notice how input_file and output_file in the following are grouped inside square brackets to indicate that they are optional as a pair. That is, if you provide input_file, you may also optionally provide output_file:

uniq [-c | -d | -u] [-i] [-f num] [-s chars] [input_file [output_file]]

For example, I can count tests/inputs/two.txt and place the output into out:

uniq−ctests/inputs/two.txtoutuniq -c tests/inputs/two.txt out cat out 2 a

With no positional arguments, uniq will read from STDIN by default:

$ cat tests/inputs/two.txt | uniq -c 2 a

If you want to read from STDIN and indicate the output filename, you must use a dash (-) for the input filename:

cattests/inputs/two.txt∣uniq−c−outcat tests/inputs/two.txt | uniq -c - out cat out 2 a

The GNU version works basically the same while also providing many more options:

$ uniq --help
Usage: uniq [OPTION]... [INPUT [OUTPUT]]

Filter adjacent matching lines from INPUT (or standard input), writing to OUTPUT (or standard output).

With no options, matching lines are merged to the first occurrence.

Mandatory arguments to long options are mandatory for short options too. -c, —count prefix lines by the number of occurrences

  • -d, —repeated only print duplicate lines, one for each group
  • -D, —all-repeated[=METHOD] print all duplicate lines
groups can be delimited with an empty line
METHOD={none(default),prepend,separate}
  • -f, —skip-fields=N avoid comparing the first N fields
  • —group[=METHOD] show all items, separating groups with an empty line METHOD={separate(default),prepend,append,both}
  • -i, —ignore-case ignore differences in case when comparing
  • -s, —skip-chars=N avoid comparing the first N characters
  • -u, —unique only print unique lines
  • -z, —zero-terminated end lines with 0 byte, not newline
  • -w, —check-chars=N compare no more than N characters in lines
  • —help display this help and exit
  • —version output version information and exit

A field is a run of blanks (usually spaces and/or TABs), then nonblank characters. Fields are skipped before chars.

Note: ‘uniq’ does not detect repeated lines unless they are adjacent. You may want to sort the input first, or use ‘sort -u’ without ‘uniq’. Also, comparisons honor the rules specified by ‘LC_COLLATE’.

As you can see, both the BSD and GNU versions have many more options, but this is as much as the challenge program is expected to implement.

This chapter’s challenge program should be called uniqr (pronounced you-need-er) for a Rust version of uniq. Start by running cargo new uniqr, then modify your Cargo.toml to add the following dependencies:

[dependencies]
clap = "2.33"
[dev-dependencies]
assert_cmd = "2"
predicates = "2"
tempfile = "3" ①
rand = "0.8"

① The tests will create temporary files using the tempfile crate (https://oreil.ly/ AYcMa).

Copy the book’s 06_uniqr/tests directory into your project, and then run cargo test to ensure that the program compiles and the tests run and fail.

Defining the Arguments

Update your src/main.rs to the following:

fn main() {
if let Err(e) = unqr::get_args().and_then(unqr::run) {
eprintln!("{}", e);
std::process::exit(1);
}
}

I suggest you start src/lib.rs with the following:

use clap::{App, Arg};
use std::error::Error;
type MyResult<T> = Result<T, Box<dyn Error>>;
#[derive(Debug)]
pub struct Config {
in_file: String, ①
out_file: Option<String>, ②
count: bool, ③
}

① This is the input filename to read, which may be STDIN if the filename is a dash.

② The output will be written either to an optional output filename or STDOUT.

③ count is a Boolean for whether or not to print the counts of each line.

Here is an outline for get_args:

pub fn get_args() -> MyResult<Config> {
let matches = App::new("uniqr")
.version("0.1.0")
.author("Ken Youens-Clark <[email protected]>")
.about("Rust uniq")
// What goes here?
.get_matches();
Ok(Config {
in_file: ...,
out_file: ...,
count: ...,
})
}

I suggest you start your run by printing the config:

pub fn run(config: Config) -> MyResult<()> {
println!("{:?}", config);
}
ok(())
}

Your program should be able to produce the following usage:

$ cargo run — -h uniqr 0.1.0 Ken Youens-Clark [email protected] Rust uniq

USAGE: uniqr [FLAGS] [ARGS]

FLAGS: -c, —count Show counts ① -h, —help Prints help information -V, —version Prints version information

ARGS: <IN_FILE> Input file [default: -] ② <OUT_FILE> Output file ③

① The -c|—count flag is optional.

② The input file is the first positional argument and defaults to a dash (-).

③ The output file is the second positional argument and is optional.

By default the program will read from STDIN, which can be represented using a dash:

Terminal window
$ cargo run
Config { in_file: "-", out_file: None, count: false }

The first positional argument should be interpreted as the input file and the second positional argument as the output file.¹ Note that clap can handle options either before or after positional arguments:

Terminal window
$ cargo run -- tests/inputs/one.txt out --count
Config { in_file: "tests/inputs/one.txt", out_file: Some("out"), count: true }

Take a moment to finish get_args before reading further.

[^1] While the goal is to mimic the original versions as much as possible, I would note that I do not like optional positional parameters. In my opinion, it would be better to have an -o|—output option that defaults to STDOUT and have only one optional positional argument for the input file that defaults to STDIN.

I assume you are an upright and moral person who figured out the preceding function on your own, so I will now share my solution:

pub fn get_args() -> MyResult<Config> {
let matches = App::new("unlq")
.version("0.1.0")
.author("Ken Youens-Clark <[email protected]>")
.about("Rust unlq")
.arg(
Arg::with_name("in_file")
.value_name("IN_FILE")
.help("Input file")
.default_value("-"),
)
.arg(
Arg::with_name("out_file")
.value_name("OUT_FILE")
.help("Output file"),
)
.arg(
Arg::with_name("count")
.short("c")
.help("Show counts")
.long("count")
.takes_value(false),
)
.get_matches();
Ok(Config {
in_file: matches.value_of_lossy("in_file").unwrap().to_string(), ①
out_file: matches.value_of("out_file").map(String::from), ②
count: matches.is_present("count"), ③
})
}

① Convert the in_file argument to a String.

② Convert the out_file argument to an Option.

③ The count is either present or not, so convert this to a bool.

Because the in_file argument has a default value, it is safe to call Option::unwrap and convert the value to a String. There are several other ways to get the same result, none of which is necessarily superior. You could use Option::map (https://oreil.ly/JaDYG) to feed the value to String::from (https://oreil.ly/X32Yh) and then unwrap it:

in_file: matches.value_of_lossy("in_file").map(String::from).unwrap(),

You could also use a closure that calls Into::into (https://oreil.ly/HTe0m) to convert the value into a String because Rust can infer the type: in_file: matches.value_of_lossy(“in_file”).map(|v| v.into()).unwrap(), The preceding can also be expressed using the Into::into function directly because functions are first-class values that can be passed as arguments: in_file: matches.value_of_lossy(“in_file”).map(Into::into).unwrap(), The out_file is optional, but if there is an option, you can use Option::map to con- vert a Some value to a String: out_file: matches.value_of(“out_file”).map(|v| v.to_string()),

Variable Lifetimes

You may wonder why I don’t leave in_file as a &str value. Consider what would happen if I did this:

#[derive(Debug)]
pub struct Config {
in_file: &str,
out_file: Option<&str>,
count: bool,
}
pub fn get_args() -> MyResult<Config> {
let matches = App::new("uniq")
...
Ok(Config {
in_file: matches.value_of("in_file").unwrap(),
out_file: matches.value_of("out_file"),
count: matches.is_present("count"),
})
}

The compiler would complain about missing lifetime specifiers:

error[E0106]: missing lifetime specifier —> src/lib.rs:11:14 | 11 | in_file: &str, | ^ expected named lifetime parameter | help: consider introducing a named lifetime parameter | 10 | pub struct Config<‘a> { 11 | in_file: &‘a str,

The lifetime refers to how long a value is valid for borrowing throughout a program. The problem here is that I’m trying to take references to values from matches, which goes out of scope at the end of the function and is then dropped (https://oreil.ly/ R2d0d). Returning a Config that stores references to a dropped value would lead to dangling pointers, which is not allowed. In the next section I’ll demonstrate a practical use of lifetimes; for a deeper discussion of lifetimes, I would refer you to other texts, such as Programming Rust or other more comprehensive books. In this instance, the only valid choice is to return a dynamic, heap-allocated String.

The test suite in tests/cli.rs is fairly large, containing 78 tests that check the program under the following conditions:

  • Input file as the only positional argument, check STDOUT
  • Input file as a positional argument with —count option, check STDOUT
  • Input from STDIN with no positional arguments, check STDOUT
  • Input from STDIN with —count and no positional arguments, check STDOUT
  • Input and output files as positional arguments, check output file
  • Input and output files as positional arguments with —count, check output file
  • Input from STDIN and output files as positional arguments with —count, check output file

Given how large and complicated the tests became, you may be interested to see how I structured tests/cli.rs, which starts with the following:

use assert_cmd::Command;
use predicates::prelude::*;
use rand::{distributions::Alphanumeric, Rng};
use std::fs;
use tempfile::NamedTempFile; ①
type TestResult = Result<(), Box<dyn std::error::Error>>;
struct Test { ②
input: &'static str,
out: &'static str,
out_count: &'static str,
}

① This is used to create temporary output files.

② A struct to define the input files and expected output values with and without the counts.

Note the use of ‘static to denote the lifetime of the values. I want to define structs with &str values, and the Rust compiler would like to know exactly how long the values are expected to stick around relative to one another. The ‘static annotation shows that this data will live for the entire lifetime of the program. If you remove it and run the tests, you’ll see similar errors from the compiler, as shown in the previous section, along with a suggestion of how to fix it:

error[E0106]: missing lifetime specifier —> tests/cli.rs:8:12 | 8 | input: &str, | ^ expected named lifetime parameter | help: consider introducing a named lifetime parameter | 7 | struct Test<‘a> { 8 | input: &‘a str,

Next, I define some constant values I need for testing:

const PRG: &str = "uniqr"; ①
const EMPTY: Test = Test {
input: "tests/inputs/empty.txt", ②
out: "tests/inputs/empty.txt.out", ③
out_count: "tests/inputs/empty.txt.c.out", ④
};

① The name of the program being tested

② The location of the input file for this test

③ The location of the output file without the counts

④ The location of the output file with the counts

After the declaration of EMPTY, there are many more Test structures followed by several helper functions. The run function will use Test.input as an input file and will compare STDOUT to the contents of the Test.out file:

fn run(test: &Test) -> TestResult { ①
let expected = fs::read_to_string(test.out)?; ②
Command::cargo_bin(PRG)? ③
.arg(test.input)
.assert()
.success()
.stdout(expected);
Ok(())
}

① The function accepts a Test and returns a TestResult.

② Try to read the expected output file. ③ Try to run the program with the input file as an argument, verify it ran successfully, and compare STDOUT to the expected value.

The run_count helper function works very similarly, but this time it tests for the counting:

fn run_count(test: &Test) -> TestResult {
let expected = fs::read_to_string(test.out_count)?; ①
Command::cargo_bin(PRG)?
.args(&[test.input, "-c"]) ②
.assert()
.success()
.stdout(expected);
Ok(())
}

① Read the Test.out_count file for the expected output.

② Pass both the Test.input value and the flag -c to count the lines.

The run_stdin function will supply the input to the program through STDIN:

fn run_stdin(test: &Test) -> TestResult {
let input = fs::read_to_string(test.input)?; ①
let expected = fs::read_to_string(test.out)?; ②
Command::cargo_bin(PRG)? ③
.write_stdin(input)
.assert()
.success()
.stdout(expected);
Ok(())
}

① Try to read the Test.input file.

② Try to read the Test.out file.

③ Pass the input through STDIN and verify that STDOUT is the expected value.

The run_stdin_count function tests both reading from STDIN and counting the lines:

fn run_stdin_count(test: &Test) -> TestResult {
let input = fs::read_to_string(test.input)?;
let expected = fs::read_to_string(test.out_count)?;
Command::cargo_bin(PRG)? ①
.arg("--count")
.write_stdin(input)
.assert()
.success()
.stdout(expected);
ok(())
}

① Run the program with the long —count flag, feed the input to STDIN, and verify that STDOUT is correct.

The run_outfile function checks that the program accepts both the input and out- put files as positional arguments. This is somewhat more interesting as I needed to use temporary files in the testing because, as you have seen repeatedly, Rust will run the tests in parallel. If I were to use the same dummy filename like blargh to write all the output files, the tests would overwrite one another’s output. To get around this, I use the tempfile::NamedTempFile (https://oreil.ly/3y9kZ) to get a dynamically gener- ated temporary filename that will automatically be removed when I finish:

fn run_outfile(test: &Test) -> TestResult {
let expected = fs::read_to_string(test.out)?;
let outfile = NamedTempFile::new()?; ①
let outpath = &outfile.path().to_str().unwrap(); ②
Command::cargo_bin(PRG)? ③
.args(&[test.input, outpath])
.assert()
.success()
.stdout("");
let contents = fs::read_to_string(&outpath)?; ④
assert_eq!(&expected, &contents); ⑤
Ok(())
}

① Try to get a named temporary file.

② Get the path (https://oreil.ly/jIpqA) to the file.

③ Run the program with the input and output filenames as arguments, then verify there is nothing in STDOUT.

④ Try to read the output file.

⑤ Check that the contents of the output file match the expected value.

The next two functions are variations on what I’ve already shown, adding in the —count flag and finally asking the program to read from STDIN when the input file- name is a dash. The rest of the module calls these helpers using the various structs to run all the tests.

I would suggest you start in src/lib.rs by reading the input file, so it makes sense to use the open function from previous chapters:

fn open(filename: &str) -> MyResult<Box<dyn BufRead>> {
match filename {
"-" => Ok(Box::new(BufReader::new(io::stdin()))),
_ => Ok(Box::new(BufReader::new(File::open(filename)?))),
}
}

Be sure you expand your imports to include the following:

use clap::{App, Arg};
use std::{ ①
error::Error,
fs::File,
io::{self, BufRead, BufReader},
};

① This syntax will group imports by common prefixes, so all the following come from std.

You can borrow quite a bit of code from Chapter 3 that reads lines of text from an input file or STDIN while preserving the line endings:

pub fn run(config: Config) -> MyResult<()> {
let mut file = open(&config.in_file)
.map_err(|e| format!("{}: {}", config.in_file, e)); ①
let mut line = String::new(); ②
loop { ③
let bytes = file.read_line(&mut line)?; ④
if bytes == 0 { ⑤
break;
}
print!("{}", line); ⑥
line.clear(); ⑦
}
Ok(())
}

① Either read STDIN if the input file is a dash or open the given filename. Create an informative error message when this fails.

② Create a new, empty mutable String buffer to hold each line.

③ Create an infinite loop.

④ Read a line of text while preserving the line endings. ⑤ If no bytes were read, break out of the loop. ⑥ Print the line buffer. ⑦ Clear the line buffer.

Run your program with an input file to ensure it works: $ cargo run — tests/inputs/one.txt a

It should also work for reading STDIN: $ cargo run — < tests/inputs/one.txt a

Next, make your program iterate the lines of input and count each unique run of lines, then print the lines with and without the counts. Once you are able to create the correct output, you will need to handle printing it either to STDOUT or a given file-name. I suggest that you copy ideas from the open function and use File::create (https://oreil.ly/QPy35).

Stop reading here and finish your program. Remember that you can run just a subset of tests with a command like cargo test empty to run all the tests with the string empty in the name.

I’ll step you through how I arrived at a solution. Your version may be different, but it’s fine as long as it passes the test suite. I decided to create two additional mutable variables to hold the previous line of text and the running count. For now, I will always print the count to make sure it’s working correctly:

pub fn run(config: Config) -> MyResult<()> {
let mut file = open(&config.in_file)
.map_err(|e| format!("{}: {}", config.in_file, e))?;
let mut line = String::new();
let mut previous = String::new(); ①
let mut count: u64 = 0; ②
loop {
let bytes = file.read_line(&mut line)?;
if bytes == 0 {
break;
}
if line.trim_end() != previous.trim_end() { ③
if count > 0 { ④
print!("{:>4} {}", count, previous); ⑤
}
previous = line.clone(); ⑥
count = 0; ⑦
}

count += 1; ⑧

line.clear();
if count > 0 { ⑨
print!("{:>4} {}", count, previous);
}
Ok(())
}

① Create a mutable variable to hold the previous line of text.

② Create a mutable variable to hold the count.

③ Compare the current line to the previous line, both trimmed of any possible trailing whitespace.

④ Print the output only when count is greater than 0.

⑤ Print the count right-justified in a column four characters wide followed by a space and the previous value.

⑥ Set the previous variable to a copy of the current line.

⑦ Reset the counter to 0.

⑧ Increment the counter by 1.

⑨ Handle the last line of the file.

bird I didn’t have to indicate the type u64 for the count variable. Rust will happily infer a type. On a 32-bit system, Rust would use an i32, which would limit the maximum number of duplicates to i32::MAX (https://oreil.ly/sE2YC), or 2,147,483,647. That’s a big number that’s likely to be adequate, but I think it’s better to have the program work consistently by specifying u64.

If I run cargo test, this will pass a fair number of tests. This code is clunky, though. I don’t like having to check if count > 0 twice, as it violates the don’t repeat yourself (DRY) principle, where you isolate a common idea into a single abstraction like a function rather than copying and pasting the same lines of code throughout a program. Also, my code always prints the count, but it should print the count only when config.count is true. I can put all of this logic into a function, and I will specifically use a closure to close around the config.count value:

let print = |count: u64, text: &str| {
if count > 0 {
if config.count {
print!("{:>4} {}", count, text);
} else {
print!("{}", text);
}
}
};

① The print closure will accept count and text values.

② Print only if count is greater than 0.

③ Check if the config.count value is true.

④ Use the print! macro to print the count and text to STDOUT.

⑤ Otherwise, print the text to STDOUT.


Closures Versus Functions

A closure is a function, so you might be tempted to write print as a function inside the run function:

pub fn run(config: Config) -> MyResult<()> {
...
fn print(count: u64, text: &str) {
if count > 0 {
if config.count {
print!("{:>4} {}", count, text);
} else {
print!("{}", text);
}
}
}
...
}
This is a common way to write a closure in other languages, and Rust does allow you
to declare a function inside another function; however, the Rust compiler specifically
disallows capturing a dynamic value from the environment:
error[E0434]: can't capture dynamic environment in a fn item
--> src/lib.rs:67:16
|
67 | if config.count {
| ^^^^^^^^^^^^^^^
|
= help: use the `|| { ... }` closure form instead

I can update the rest of the function to use this closure:

loop {
let bytes = file.read_line(&mut line)?;
if bytes == 0 {
break;
}
if line.trim_end() != previous.trim_end() {
print(count, &previous);
previous = line.clone();
count = 0;
}
count += 1;
line.clear();
}
print(count, &previous);
At this point, the program will pass several more tests. All the failed test names have
the string outfile because the program fails to write a named output file. To add this
last feature, you can open the output file in the same way as the input file, either by
creating a named output file using File::create or by using std::io::stdout. Be
sure to add use std::io::Write for the following code, which you can place just
after the file variable:
let mut out_file: Box<dyn Write> = match &config.out_file {
Some(out_name) => Box::new(File::create(out_name)?), ②
_ => Box::new(io::stdout()), ③
};

① The mutable out_file will be a boxed value that implements the std::io

::Write trait (https://oreil.ly/Hlk6A).

② When config.out_file is Some filename, use File::create (https://oreil.ly/ QPy35) to try to create the file.

③ Otherwise, use std::io::stdout (https://oreil.ly/gjxor).

If you look at the documentation for File::create and io::stdout, you’ll see both have a “Traits” section showing the various traits they implement. Both show that they implement Write, so they satisfy the type requirement Box, which says that the value inside the Box must implement this trait.

The second change I need to make is to use out_file for the output. I will replace the print! macro with write! (https://oreil.ly/oiJaM) to write the output to a stream like a filehandle or STDOUT. The first argument to write! must be a mutable value that implements the Write trait. The documentation shows that write! will return a std::io::Result because it might fail. As such, I changed my print closure to return MyResult. Here is the final version of my run function that passes all the tests:

pub fn run(config: Config) -> MyResult<()> {
let mut file = open(&config.in_file)
.map_err(|e| format!("{}: {}", config.in_file, e)); ①
let mut out_file: Box<dyn Write> = match &config.out_file { ②
Some(out_name) => Box::new(File::create(out_name)?),
_ => Box::new(io::stdout()),
};
let mut print = |count: u64, text: &str| -> MyResult<()> { ③
if count > 0 {
if config.count {
write!(out_file, "{:>4} {}", count, text)?;
} else {
write!(out_file, "{}", text)?;
}
};
Ok(())
};
let mut line = String::new();
let mut previous = String::new();
let mut count: u64 = 0;
loop {
let bytes = file.read_line(&mut line)?;
if bytes == 0 {
break;
}
if line.trim_end() != previous.trim_end() { ④
print(count, &previous)?;
previous = line.clone();
count = 0;
}
count += 1;
}
line.clear();
}
print(count, &previous)?; ⑤
ok(())
}

① Open either STDIN or the given input filename.

② Open either STDOUT or the given output filename.

③ Create a mutable print closure to format the output.

④ Use the print closure to possibly print output. Use ? to propagate potential errors.

⑤ Handle the last line of the file.

Note that the print closure must be declared with the mut keyword to make it mutable because the out_file filehandle is borrowed. Without this, the compiler will show the following error:

error[E0596]: cannot borrow `print` as mutable, as it is not declared as mutable
--> src/lib.rs:84:13
|
63 | let print = |count: u64, text: &str| -> MyResult<()> {
| ---- help: consider changing this to be mutable: `mut print`
...
66 | write!(out_file, "{:>4} {}", count, text)?;
| --------- calling `print` requires mutable binding
| due to mutable borrow of `out_file`

Again, it’s okay if your solution is different from mine, as long as it passes the tests. Part of what I like about writing with tests is that there is an objective determination of when a program meets some level of specifications. As Louis Srygley once said, “Without requirements or design, programming is the art of adding bugs to an empty text file.”² I would say that tests are the requirements made incarnate. Without tests, you simply have no way to know when a change to your program strays from the requirements or breaks the design.

2 Programming Wisdom (@CodeWisdom), “Without requirements or design, programming is the art of adding bugs to an empty text file.” - Louis Srygley,” Twitter, January 24, 2018, 1:00 p.m., https://oreil.ly/FC6aS.

Going Further

Can you find other ways to write this algorithm? For instance, I tried another method that read all the lines of the input file into a vector and used Vec::windows (https://oreil.ly/vudZO) to look at pairs of lines. This was interesting but could fail if the size of the input file exceeded the available memory on my machine. The solution presented here will only ever allocate memory for the current and previous lines and so should scale to any size file.

As usual, the BSD and GNU versions of uniq both have many more features than I chose to include in the challenge. I would encourage you to add all the features you would like to have in your version. Be sure to add tests for each feature, and always run the entire test suite to verify that all previous features still work.

In my mind, uniq is closely tied with sort, as I often use them together. Consider implementing your own version of sort, at least to the point of sorting values lexicographically (in dictionary order) or numerically.

Summary

In about 100 lines of Rust, the uniqr program manages to replicate a reasonable subset of features from the original uniq program. Compare this to the GNU C source code (https://oreil.ly/X8ipY), which has more than 600 lines of code. I would feel much more confident extending uniqr than I would using C due to the Rust compiler’s use of types and useful error messages.

Let’s review some of the things you learned in this chapter:

  • You can now open a new file for writing or print to STDOUT.
  • DRY says that any duplicated code should be moved into a single abstraction like a function or a closure.
  • A closure must be used to capture values from the enclosing scope.
  • When a value implements the Write trait, it can be used with the write! and writeln! macros.
  • The tempfile crate helps you create and remove temporary files.
  • The Rust compiler may sometimes require you to indicate the lifetime of a variable, which is how long it lives in relation to other variables.

In the next chapter, I’ll introduce Rust’s enumerated enum type and how to use regular expressions.