# Custom format specifier =\> trait pairs

**URL:** <https://internals.rust-lang.org/t/custom-format-specifier-trait-pairs/8815>\
**Category:** language design\
**Created:** [November 11, 2018, 5:58pm UTC](https://internals.rust-lang.org/t/custom-format-specifier-trait-pairs/8815 "2018-11-11T17:58:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![OJFord](https://avatars.discourse-cdn.com/v4/letter/o/7cd45c/32.png) [@OJFord](https://internals.rust-lang.org/u/OJFord)\
**Post date:** [November 11, 2018, 5:58pm UTC](https://internals.rust-lang.org/t/custom-format-specifier-trait-pairs/8815/1 "2018-11-11T17:58:44Z")

</div>

In [asking for help with an application](https://www.reddit.com/r/rust/comments/9w59di/supporting_multiple_display_formats/), I wondered ~~aloud~~ in text whether (or perhaps assumed) it were possible to have:

```
trait DisplayType1 { ... }
trait DisplayType2 { ... }
// ...

```

and then somehow register that `{:T1}` and `{:T2}` should be handled by those traits.

cf. “[The current mapping of types to traits](https://doc.rust-lang.org/std/fmt/#formatting-traits)”

In my case, I wanted for a CLI to have a human-readable ‘pretty’ output (OK, `Display` works fine here) as well as JSON output (that should be different to `Debug`) and perhaps others in the future (e.g. one might imagine YAML, none, coloured, not).

It was suggested that I bring this up here, so here I am. (Note I’m not here to repeat my ‘what should I do’ question, just to identify a use case for something like this in the language.)

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [November 11, 2018, 6:03pm UTC](https://internals.rust-lang.org/t/custom-format-specifier-trait-pairs/8815/2 "2018-11-11T18:03:58Z")

</div>

It should be possible to write your own macros that interpret just your custom format specifiers, and then pass on the rest to the standard format/print macros. That’d probably be the ideal way to test how useful/scalable this design is before any formal proposal.

---

<div class="post-metadata">

**Author:** ![H2CO3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/h2co3/32/2849_2.png) [@H2CO3](https://internals.rust-lang.org/u/H2CO3)\
**Post date:** [November 11, 2018, 6:25pm UTC](https://internals.rust-lang.org/t/custom-format-specifier-trait-pairs/8815/3 "2018-11-11T18:25:52Z")

</div>

I think this is a perfect job for Newtypeman!

You can define a newtype around the value/type you want to display, then implement `Display` for it in the way you deem appropriate. A perfect example of this is [the `adapter` module of the UUID crate](https://docs.rs/uuid/0.7.1/uuid/adapter/index.html).

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:31am UTC](https://internals.rust-lang.org/t/custom-format-specifier-trait-pairs/8815/4 "2019-03-25T08:31:08Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
