#![allow(dead_code, deprecated, unused_variables, unused_mut)] use std::fmt; struct Foo(i32); impl fmt::Display for Foo { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { if let Some(width) = formatter.width() { write!(formatter, "{:width$}", format!("Foo({})", self.0), width = width) } else { write!(formatter, "Foo({})", self.0) } } } fn main() { assert_eq!(format!("{:10}", Foo(23)), "Foo(23) "); assert_eq!(format!("{}", Foo(23)), "Foo(23)"); }