I was working with a custom error type defined as an enum and ran into an issue where printing it would cause my stack to overflow. I took the problem code and reduced it down to the simplest version that would cause the error.
It looks like the issue was in implementing the Display trait and writing the enum itself, instead of one of the values.
pub enum TestEnum {
TestValue,
}
impl fmt::Display for TestEnum {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "TestEnum: {}", *self)
}
}
Whole program here - Rust Playground
I was able to fix the issues by only printing values of the enum, not the enum itself.
My question is why does this happen? It isn't immediately obvious that this would cause an infinite recursive print and the compiler doesn't give any warnings.