impl<T: Display> Display for Option<T>?

At the moment, impl<T: Debug> Debug for Option<T> exists, but impl<T: Display> Display for Option<T> doesn’t. Would it make sense to implement that?

The natural thing to do is:

  • in the Some case, just call the Display impl on T
  • in the None case it wouldn’t display anything

Users would be free to override this by using a newtype wrapper (that’s what they have to do today anyway).

Generally it seems that std doesn’t define Display for things where there are multiple reasonable choices, which is fairly often since humans are complicated :wink:

For the specific proposal given, it seems unfortunate that Some("").to_string() and None.to_string() would produce the same thing. And the best way to solve that seems like it’s context-specific, at which point not having the impl is just as good.

10 Likes

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