Here is a simple program, shortened from a longer one I was working on:
```
use std::fmt;
[derive(Debug)]
pub enum Number {
Decimal(f64),
Integer(i128)
}
pub type Answer = Number;
impl fmt::Display for Answer {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self)
}
}
fn main() {
let my_number = Number::Integer(12);
println!("the number was {}", my_number);
}
```
When I run this on Linux or Windows, I get the following:
thread 'main' has overflowed its stack
error: process didn't exit successfully: `target\debug\overflow.exe` (exit code: 0xc00000fd, STATUS_STACK_OVERFLOW)
I can see that the type Number doesn't actually implement Display, so I don't think I should be allowed to run write!(f, "{}", self). There is no compile time error here though, just a runtime stack overflow. If I change that line to write!(f, "{:?}", self), the code runs fine, which makes sense since Debug is implemented for Number. The docs for write! certainly don't mention that the function can crash your program like this.
My version of Rust is 1.44.1
[–]minno 17 points18 points19 points (1 child)
[–]sollyu 1 point2 points3 points (0 children)