Separate the symptom from its cause
For a panic in a local project, run RUST_BACKTRACE=1 cargo test to request a backtrace. Find the relevant frame in your code, then inspect the values that reached it. A panic location tells you where execution stopped; it does not prove that the bad assumption began there.
For a returned Err, inspect the error and the operation that produced it. Result represents a failure the caller can handle. Replacing every error with unwrap() changes failures into panics and removes the opportunity to report or recover deliberately.
Make numeric boundaries part of the contract
Consider a buffer size calculated from a header length and payload length. Checking the total after an overflowing addition is too late. checked_add returns None when the sum cannot fit, so the caller can handle that boundary explicitly.
This standalone example uses None for an unrepresentable total. It tests the ordinary case, an empty payload, and the largest boundary without allocating a buffer. In a real API, choose an error that communicates the failure to its caller.
fn frame_size(header: usize, payload: usize) -> Option<usize> {
header.checked_add(payload)
}
fn main() {
assert_eq!(frame_size(4, 8), Some(12));
assert_eq!(frame_size(4, 0), Some(4));
assert_eq!(frame_size(usize::MAX, 1), None);
}Distinguish bytes from characters
Rust strings contain UTF-8. A byte offset may fall inside a character, so a range that works for ASCII may panic for other text. When investigating a text failure, record the input, byte length, and the exact range being used. Decide whether the requirement is about bytes, Unicode scalar values, or user-perceived characters.
The str::get method returns None for an invalid range instead of panicking. Iterating chars() counts Unicode scalar values, which is different from counting grapheme clusters. Choose behavior from the product requirement before choosing a string operation.
Test sequences as well as individual calls
A streaming parser may behave correctly when a record arrives in one chunk and fail when that record is split. Use the same logical input with different chunk boundaries, including empty chunks if the interface permits them. Compare the final output and error count, not only the value returned by a single call.
In Bugger, use visible output and custom cases to build your reproduction, then submit the fix against additional regressions. Backtrace commands and interactive tools described above are for your local Rust project.
- Keep the smallest input or event sequence that reproduces the failure.
- Test empty input, maximum values, and non-ASCII text when relevant.
- Preserve error handling while changing the failing logic.
- Verify both the failing case and an ordinary successful case.
Frequently asked questions
- How do you get a full stack trace when a Rust test panics?
- Run `RUST_BACKTRACE=1 cargo test` (or `RUST_BACKTRACE=full` for every frame) to see the exact call path leading to the panic.
- Why does slicing a Rust &str panic on non-ASCII text?
- Rust string slices index by UTF-8 byte offsets, not characters. Slicing at a byte index inside a multi-byte UTF-8 character panics; use `str::get` or `char_indices()` instead.
Further reading
Start with primary reports and documentation. Community discussions are discovery leads, not evidence on their own.