When to derive Debug and when to use custom implementation

⚓ Rust    📅 2026-05-28    👤 surdeus    👁️ 3      

surdeus

Given a type in a library whose internal representation is not exposed. I am always wondering should I "expose" the internal representation via deriving Debug or should I have a custom Debug implementation.

I have looked at some examples in the std and I found two interesting cases that seem to contradict each other:

std::time::Duration is internally represented by secs and nanos integers, but Debug has a custom implementation, so for example

println!("{:?}", std::time::Duration::new(100, 5000));

prints

100.000005s

On the other hand std::time::SystemTime has a platform-dependent internal representation but the Debug for

println!("{:?}", std::time::SystemTime::now());

prints

SystemTime { tv_sec: 1779995532, tv_nsec: 81737297 }

So there it exposes the sec and nanosec integers in some sense. So why have a human readable representation for Duration but a quite similar representation than the internals of Duration for SystemTime? Is there a general rule of thumb when to derive Debug and when to try to make it human readable by a custom implementation?

2 posts - 2 participants

Read full topic

🏷️ Rust_feed