How to improve Rust `Result` stack tracing?

59 points Mercerenies

In another thread, it was mentioned how good Rust compiler errors are compared to C++ template error messages, which is completely true. But it got me thinking: One thing that Rust lacks, in my mind, is good tracebacks on Result values at runtime. In Python, for instance, if I throw an exception and it bubbles up a few stack frames, I can still see every stack frame it had when it was thrown. If I print it out, I get frankly beautiful output.

The same is true of Rust panics (at least on debug builds). But for Result, if I propagate an Err up a few stack frames (using ?), I lose that stack information. Printing an Err via Debug just prints a description of what went wrong, not where. From what I can tell, anyhow helps with this a bit: Any errors which are of type anyhow::Error automagically store their own stack trace, but that would require me to use anyhow::Error everywhere, while I really only want to use that for my top-level.

What does everyone else on here do to recover good stack traces on Err values?