# Rationale about not impl Display for unit type ()

**URL:** <https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051>\
**Category:** libs\
**Created:** [July 21, 2022, 2:10am UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051 "2022-07-21T02:10:30Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stargateur](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/stargateur/32/9385_2.png) [@Stargateur](https://internals.rust-lang.org/u/Stargateur)\
**Post date:** [July 21, 2022, 2:10am UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/1 "2022-07-21T02:10:30Z")

</div>

I didn't find any rationale about why we didn't have a `Display` implementation of unit. I run into this cause in some generic API I impl a trait for `()` that represent "Ignore Error" so the type of `Error` is `()`, I mandate all `Error` impl `Display` but so `()` is not `Display`. That not a problem, I just declared that `Error` associate type of `()` is something else. But that raise my curiosity about Display and unit. I search in reddit ,issues, forum, stack overflow, duckduckgo, google, everywhere I could I found no talk about it.

Some found:

- [Rust : Solving `()` doesn't implement `std::fmt::Display` error in Closure](https://www.howtosolutions.net/2020/12/rust-closure-compiler-error-unit-type-not-implementing-std-fmt-display-trait/)
- [`()` doesn't implement `std::fmt::Display` · Issue #5 · actix/sockjs · GitHub](https://github.com/actix/sockjs/issues/5)

I don't known if it would be a good thing to implement Display, first unit is not universal, there is no obvious way to represent it outside of Rust, however that also mean Rust could choice how unit should be display. It's nice for generic.

"unit" versus "()" I guess. What do you think, could we add it, is it a good thing ?

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [July 21, 2022, 2:37am UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/2 "2022-07-21T02:37:35Z")

</div>

One relevant aspect of `Display` is that it comes with an automatic `to_string` method. I don't quite see how `to_string` feel all that appropriate on `()`; but of course feel free to disagree 🙂

---

<div class="post-metadata">

**Author:** ![chrefr](https://avatars.discourse-cdn.com/v4/letter/c/e480ec/32.png) [@chrefr](https://internals.rust-lang.org/u/chrefr)\
**Post date:** [July 21, 2022, 11:34am UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/3 "2022-07-21T11:34:38Z")

</div>

How do you print a `()` to the user? Most likely empty string, but I don't think this is applicable to all cases.

---

<div class="post-metadata">

**Author:** ![danjl1100](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danjl1100/32/6466_2.png) [@danjl1100](https://internals.rust-lang.org/u/danjl1100)\
**Post date:** [July 21, 2022, 12:05pm UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/4 "2022-07-21T12:05:14Z")

</div>

For better or for worse, for the situation you described I've been using my own `Never` type (since one is not yet stabilized in `std` as I recall)

To me, this encodes the uninhabited nature of an error that does not occur (so may fit your idea of an error that is always, by definition, ignored)

```rust
enum Never {}
impl Display for Never {
    fn fmt(&self, _: &mut fmt::Formatter) -> fmt::Result {
        match *self {}
    }
}

```

[Playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3cac44b40db2f9bf224989a48cd6a790)

---

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [July 21, 2022, 3:58pm UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/5 "2022-07-21T15:58:39Z")

</div>

Side note: [`std::convert::Infallible`](https://doc.rust-lang.org/std/convert/enum.Infallible.html)

---

<div class="post-metadata">

**Author:** ![felix.s](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/felix.s/32/8073_2.png) [@felix.s](https://internals.rust-lang.org/u/felix.s)\
**Post date:** [July 21, 2022, 4:17pm UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/6 "2022-07-21T16:17:36Z")

</div>

> [@Stargateur](#):
>
> unit is not universal, there is no obvious way to represent it outside of Rust

I think that’s a sufficient reason not to implement `Display` for `()`: because it’s impossible to present the value of the unit type to the user without falling into nerdview.

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [July 21, 2022, 5:46pm UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/7 "2022-07-21T17:46:27Z")

</div>

And consider in particular the use case of an error value. (`()` also doesn't implement `std::error::Error`, but let's ignore that). What should the text be even in that case?

It can't be the empty string, because then programs would print messages like

> `Error: `

which aren't very friendly. It could be something like “`no further information is available`”, but then the standard library is making a specific arbitrary choice of wording that might not make sense for other applications.

As I see it, `Display` should be implemented for types which have a _single obvious choice_ of string representation. ("Obvious" in that there can be others; for example, numbers can be printed in many bases but the world has standardized on base 10.) `()` does not have such a single choice; the correct representation of it is context-dependent.

---

<div class="post-metadata">

**Author:** ![afetisov](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/afetisov/32/8508_2.png) [@afetisov](https://internals.rust-lang.org/u/afetisov)\
**Post date:** [July 21, 2022, 5:58pm UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/8 "2022-07-21T17:58:52Z")

</div>

The solution for your problem is that, rather than using an empty tuple `()`, to create your own zero-sized error type

```rust
pub struct SomethingIsWrong;

impl Display for SomethingIsWrong { ... }

```

You avoid the issues with `()`, get a clear name for the error type, and can implement Display however you want. You can also declare many different error types for different error cases, and no one needs to guess "what did `()` as an error stand for?".

Just as, most of the time, you should prefer explicit structs with explicit field names instead of nameless tuples, you should prefer custom zero-sized structs to `()`. The primary reason to use `()` is as a special case of the more general n-ary tuples. This is common inside of macros, or when doing type-level programming.

---

<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:** [October 19, 2022, 5:58pm UTC](https://internals.rust-lang.org/t/rationale-about-not-impl-display-for-unit-type/17051/9 "2022-10-19T17:58:53Z")

</div>

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