With input from any BufRead and output to any Write, you can run a whole session of the program without a person at the keyboard. A fixture is a saved input together with the result you expect from it. A transcript check replays the fixture into a Vec<u8> and compares every byte of the output with the expected text: each success line, each error line, its line number, the order, and the final newline.
Why compare everything instead of checking a few interesting lines? Because the bugs that slip through in an input loop are rarely in one place. The parser can be right and the loop can still stop early, skip a line, or number an error wrongly. A transcript covers how parsing, iteration and output work together, so when you change one part later, the fixture tells you whether anything else moved. That is what makes it a regression check: it guards behavior you already had.
This lesson also pins down a detail that is easy to get wrong. The line number in an error refers to the line in the input, counting blank lines, not to how many commands were accepted. Call enumerate() on all lines first, and skip blank lines afterward.
Predict and run
The fixture is list, a blank line, wrong, then list. Which line number appears in the rejection? Does the last input line still run after the error?
use std::io::{self, BufRead, Cursor, Write};
fn run(input: impl BufRead, mut output: impl Write) -> io::Result<()> {
for (index, line) in input.lines().enumerate() {
let line = line?;
match line.trim() {
"" => {}
"list" => writeln!(output, "Ready")?,
_ => writeln!(output, "Line {}: unknown command", index + 1)?,
}
}
Ok(())
}
fn main() -> io::Result<()> {
let mut output = Vec::new();
run(Cursor::new(b"list\n\nwrong\nlist"), &mut output)?;
let text = String::from_utf8(output).expect("report writes UTF-8");
assert_eq!(text, "Ready\nLine 3: unknown command\nReady\n");
print!("{text}");
Ok(())
}Check your prediction
Ready
Line 3: unknown command
Readywrong is on line 3 because the blank line still received index 1 before the match skipped it; index + 1 turns the zero-based index into a human line number. The final Ready proves the loop continued after the error. Change the error branch to return Ok(()) after writing its message, and predict what happens: the last Ready disappears, and the assert_eq! stops the program. Restore the branch.
Check the whole string, not pieces
A check that only looks for the error line is easy to write, and it passes for the early-return bug you just tried. The example below runs that buggy version and compares two kinds of check side by side. It prints the results instead of asserting, so you can see both.
use std::io::{self, BufRead, Cursor, Write};
fn stops_early(input: impl BufRead, mut output: impl Write) -> io::Result<()> {
for (index, line) in input.lines().enumerate() {
match line?.trim() {
"" => {}
"list" => writeln!(output, "Ready")?,
_ => {
writeln!(output, "Line {}: unknown command", index + 1)?;
return Ok(());
}
}
}
Ok(())
}
fn main() -> io::Result<()> {
let mut output = Vec::new();
stops_early(Cursor::new(b"list\n\nwrong\nlist"), &mut output)?;
let text = String::from_utf8(output).expect("report writes UTF-8");
println!("Mentions line 3: {}", text.contains("Line 3: unknown command"));
println!("Transcript matches: {}", text == "Ready\nLine 3: unknown command\nReady\n");
Ok(())
}Mentions line 3: true
Transcript matches: falseThe partial check says everything is fine; the transcript check catches the lost command. When a real assert_eq! on two strings fails, Rust prints both values as left and right, so compare them line by line to find the first difference. For a long expected transcript, the Day 7 checks use concat! to write one string literal per output line.
Number lines before you skip any
The order of enumerate and the blank-line test matters. If you remove blank lines first, the numbers count only the lines that survived, and the error points at the wrong place in the input. A &str has its own lines() method, which makes the difference easy to see without a reader:
fn main() {
let input = "list\n\nwrong";
for (index, line) in input.lines().enumerate() {
if line == "wrong" {
println!("Numbered first: line {}", index + 1);
}
}
for (index, line) in input.lines().filter(|line| !line.is_empty()).enumerate() {
if line == "wrong" {
println!("Filtered first: line {}", index + 1);
}
}
}Numbered first: line 3
Filtered first: line 2Someone opening the input file and going to line 2 would find a blank line. Only the first version tells the truth. A fixture with a blank line before an error is the cheapest way to lock that behavior in.
Write the expected transcript from the rules
Write the expected text from the behavior you promised, before you run the program. If you copy whatever the program prints into the assertion, the check only proves the program does what it already did, bugs included.
Practice
Your turn
Write run so it prints Found list for a line that says list, Ignored for any other nonblank line, and nothing for a blank line. Replay the fixture b"skip\n\nlist", which is skip, a blank line, then list. Assert that the transcript is exactly "Ignored\nFound list\n", including the final newline, then print it.
use std::io::{self, BufRead, Cursor, Write};
fn run(input: impl BufRead, mut output: impl Write) -> io::Result<()> {
// Read each line and write its result.
Ok(())
}
fn main() -> io::Result<()> {
let mut output = Vec::new();
run(Cursor::new(b"skip\n\nlist"), &mut output)?;
// Check and print the captured transcript.
Ok(())
}Hint 1: the idea
Build the expected transcript from the rules in the task: one output line for each nonblank input line, in order, and nothing for the blank one.
Hint 2: a first step
Match on line?.trim() with three arms: "", "list", and _. Then use assert_eq! on the captured text and the complete expected string.
Worked solution
use std::io::{self, BufRead, Cursor, Write};
fn run(input: impl BufRead, mut output: impl Write) -> io::Result<()> {
for line in input.lines() {
match line?.trim() {
"" => {}
"list" => writeln!(output, "Found list")?,
_ => writeln!(output, "Ignored")?,
}
}
Ok(())
}
fn main() -> io::Result<()> {
let mut output = Vec::new();
run(Cursor::new(b"skip\n\nlist"), &mut output)?;
let text = String::from_utf8(output).expect("report writes UTF-8");
assert_eq!(text, "Ignored\nFound list\n");
print!("{text}");
Ok(())
}Ignored
Found listThis fixture is small but covers three behaviors: a blank line that must produce nothing, an Ignored line that must not end the loop, and a final list without a trailing newline that must still be read. The checkpoint next combines these with a counter that a bad line must not reset.
Completion is saved in this browser only. It does not measure mastery.