4 ms·
Why isn't `Result<T, E>` just `Result<T, E> where E: std::Error`? I love rust, but I hate 'no from<lib::Error> implementation for my::Error' et al. 99.99% of t
by OJFord 4y ago
Why isn't `Result<T, E>` just `Result<T, E> where E: std::Error`?
I love rust, but I hate 'no from<lib::Error> implementation for my::Error' et al. 99.99% of the time I just want to bubble up a string to print from anything I'm using that might error.
I've used anyhow and friends, it just still requires more from me than I think should necessary.
But this is an honest question, several people put more thought into it than I have and decided it should be so, why?
- steveklabnik 4y agoOne good reason to not do this is it's std::error::Error, but there is no corresponding core::error::Error. no_std needs to be able to use Result too.
- deleted 4y ago[deleted]
- OJFord 4y agoThat feels a bit incomplete to me - that explains the language mechanism existing for using something different, but not std's lack of something more specific? As in, std::result could use std::error::Error to specialise core::result's more generic Result? And it's barely any extra work at all for a maintainer of a crate with a `no_std` feature, since you can still use the same Result; it's no longer required that its Error type implements std's, but it can?
- steveklabnik 4y agoRust doesn't yet have specialization. It is used internally by the standard library, but only for optimizations, as far as I know.
- kccqzy 4y agoThere are plenty of cases where the Err alternatives of Result doesn't actually represent an error, just a case to be handled differently. For example the binary search function (https://doc.rust-lang.org/std/primitive.slice.html#method.binary_search https://doc.rust-lang.org/std/primitive.slice.html#method.bi...) returns an index of the insertion position when the search fails. That's not usually an error to be surfaced. I find that this distinction is basically the application vs library distinction in the article. Application developers tend to "just want to bubble up a string" whereas library developers tend not to.
- woodruffw 4y agoThis is a good reason (for the purposes of stability), but IMO a misuse of `Result` in the standard library. It really ought to be something custom, closer perhaps to `ControlFlow`[1] (which is newer). But even that's not a great fit. [1]: https://doc.rust-lang.org/std/ops/enum.ControlFlow.html https://doc.rust-lang.org/std/ops/enum.ControlFlow.html