Support Alternate {:#} Mode In Display
Check Formatter::alternate() inside a custom std::fmt::Display impl so {} emits a compact one-line summary while {:#} prints a multi-line breakdown.
Code
use std::fmt;
struct EndpointReport {
service: &'static str,
status: u16,
latency_ms: u32,
region: &'static str,
}
impl fmt::Display for EndpointReport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
writeln!(f, "EndpointReport {{")?;
writeln!(f, " service: {},", self.service)?;
writeln!(f, " status: {},", self.status)?;
writeln!(f, " latency: {}ms,", self.latency_ms)?;
write!(f, " region: {}\n}}", self.region)
} else {
write!(f, "{} [{}] {}ms ({})", self.service, self.status, self.latency_ms, self.region)
}
}
}
fn main() {
let report = EndpointReport {
service: "checkout-api",
status: 200,
latency_ms: 18,
region: "eu-west-1",
};
println!("compact: {report}");
println!("expanded:\n{report:#}");
}Logic trace — Support Alternate {:#} Mode In Display
Follow the execution flow beginning at helpers fmt and main, doing all the work.
What to watch for
- Exclusively built-in runtime APIs so zero external package supply chains appear.
Putting it to use
The entry-point signature is fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
Logged output lines emit labels — compact:, expanded:\n — when the file runs.
Core lines
writeln!(f, " latency: {}ms,", self.latency_ms)?;
writeln!(f, " service: {},", self.service)?;
writeln!(f, " status: {},", self.status)?;
Interactive Tools for This Topic
Duration HumanizerTurn a number of seconds into a readable duration such as 1 day, 2 hours, 3 minutes, and show the equivalent in minutes and hours.Git Conventional Commit BuilderBuild and validate Conventional Commits v1.0.0 messages with type, scope, breaking markers, 72-char subject counter, body, footers, and git CLI command.Glob Pattern TesterCompile glob patterns with *, **, ?, character classes, and braces into regex and test them against a list of candidate file paths.Indentation ConverterConvert leading indentation between tabs and spaces at a chosen width, leaving inner content untouched.
FAQ
- What does Support Alternate {:#} Mode In Display print when run?
- Running the file directly is enough — expect labeled output lines such as `compact:`, `expanded:\n`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.