Pre-RFC bury Error::description

std::error::Error trait defines fn description(&self) -> &str method, which is not very useful.

  • It can’t (easily) allocate a formatted string, so in practice it’s just used to return a static string vaguely describing the error without any specific per-instance information.
  • fmt::Display is much more useful for errors and it can provide precise description even without allocating.

So currently we have a situation where every implementor of Error spends time creating a method that has minimal utility and generally shouldn’t be used. New users lose a battle with the borrow checker here trying to return format!(). A second description has to be written in Display. That’s a waste of everyone’s time.

I suggest:

  1. Documenting that use of this method is discouraged. Not formally deprecated in form of shouty #[deprecated] attr, just steer user away from it.

  2. Provide default implementation in the stdlib, so that this method effectively becomes optional and users can pretend it doesn’t exist if they want to.
    The default implementation could return std::intrinsics::type_name::<Self>(), which would preserve some minimal utility, but I wouldn’t mind returning anything else like "unspecified error" or ":skull_and_crossbones:"

22 Likes

I already have a bug open for #1, I just haven’t gotten to it yet.

OK, so I’ve posted it as a RFC: https://github.com/rust-lang/rfcs/pull/2230

3 Likes

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