Is there any way to only override one field when using "debug_struct"?

⚓ rust    📅 2025-05-20    👤 surdeus    👁️ 3      

surdeus

Warning

This post was published 40 days ago. The information described in this article may have changed.

For example, I have a struct with a lot of fields:

struct Foo {
  a: usize,
  b: usize,
  ...
  z: usize,
}

And I want custom the debug output for only field "m", and leave other fields as the default behavior, is it possible?

Currently When I implement the Debug trait for Foo , I need to manually write all the fields:

f.debug_struct("Foo")
.field("a", &self.a)
.field("b", &self.b)
.field("c", &self.c)
...
.field("m", &"blahblah")
...
.field("y", &self.y)
.field("z", &self.z)
.finish()

which are way too cumbersome.
P.S. To use a type wrapper for field "m" and impl Debug for this new type is not acceptable in my case.

4 posts - 4 participants

Read full topic

🏷️ rust_feed