Sub topic 14
Building a minimal reproduction
This chapter assumes you can read a rustc diagnostic, run cargo check, and look up an error code with rustc --explain. Those skills are covered in earlier chapters. Here you learn what to do when an error still makes no sense: shrink the program until only the cause remains.
Why a minimal reproduction helps
A minimal reproduction is the smallest complete program that still produces the error you are stuck on. It helps in two ways.
- Removing code often reveals the cause. Many bugs disappear when you delete the right line, and that line is the answer.
- If you still need help, a short program lets others see the problem at once. A 40-line example gets more useful replies than a link to a whole project.
The spine ties this to two earlier ideas. Fix cascading errors first, so you are shrinking a program with one real error. The error code tells you which kind of error to keep while you cut.
The shrinking procedure
- Fix or ignore every error except the one you are chasing. Start from the first error reported.
- Note its code, for example E0502.
- Copy the failing function into a new file or a new project made with
cargo new repro. - Run
cargo check. Confirm the same error code appears. - Delete one piece at a time: unused fields, extra functions, other branches. Run
cargo checkafter each deletion. - If the error changes or disappears, undo the last deletion. That piece matters.
- Replace real types with simple ones such as
i32,String, orVec<i32>, and remove external crates where possible. - Stop when removing anything more changes the error.
A worked example
Suppose a larger program has this function, and rustc reports E0502.
use std::collections::HashMap;
struct Inventory {
items: HashMap<String, Vec<u32>>,
log: Vec<String>,
}
impl Inventory {
fn restock(&mut self, name: &str) {
let counts = self.items.get(name).unwrap();
self.items.insert(String::from("spare"), vec![1]);
println!("{}", counts.len());
self.log.push(format!("restocked {name}"));
}
}
fn main() {
let mut inv = Inventory {
items: HashMap::new(),
log: Vec::new(),
};
inv.items.insert(String::from("bolt"), vec![10]);
inv.restock("bolt");
}Remove log and the push line, then the main body detail. The error stays. Replace the struct with a plain map and the keys with simple values:
use std::collections::HashMap;
fn main() {
let mut map: HashMap<i32, Vec<u32>> = HashMap::new();
map.insert(1, vec![10]);
let counts = map.get(&1).unwrap();
map.insert(2, vec![1]);
println!("{}", counts.len());
}This still gives E0502: counts borrows map, and map.insert needs a mutable borrow while counts is used later. Now the cause is plain. The struct, the string keys, and the log were never involved. The fix is to end the borrow before the insert, for example by reading let n = map.get(&1).unwrap().len(); first.
Asking for help
When you post a reproduction, include:
- The complete program, which compiles as a single file.
- The full compiler output, copied as text, not a screenshot.
- Your Rust version from
rustc --version. - What you expected, and what you already tried.
Exercise
You cut a function down and the E0382 error you were chasing changes to E0425. What should you do, and why?
Show answer
Undo the last deletion. E0425 means a name can no longer be found, so you removed a definition the code needs. The reproduction must keep the original error code (E0382), so restore that piece and try removing something else.