Recursive calls in formatting trait implementations (e.g., Display
) will lead to infinite recursion and a stack overflow.
Code examples
Noncompliant code example
use std::fmt;
struct Structure(i32);
impl fmt::Display for Structure {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_string()) // Noncompliant
}
}
Compliant solution
use std::fmt;
struct Structure(i32);
impl fmt::Display for Structure {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0) // Compliant
}
}