3 ms·
Sorry, I'm trying very hard to learn Rust, and I'm willing to accept a lot of its restrictions, but this didn't clear anything up. Why does every library imple
by rspeer 9y ago
Sorry, I'm trying very hard to learn Rust, and I'm willing to accept a lot of its restrictions, but this didn't clear anything up.
Why does every library implement its own Error type? It feels like reinventing the wheel, and it takes a lot of boilerplate code.
If you need to distinguish different kinds of errors, why not, for example, have a lot of useful pre-defined error types like Python does?
- Rusky 9y agoYou can definitely reuse other library's error types. I often do that while prototyping. The benefit of lib-specific error types, though, is that they can be far more specific.
- burntsushi 9y agoAs an example of the kind of specificity you might want, take a look at the error type for parsing a regular expression: https://docs.rs/regex-syntax/0.4.0/regex_syntax/enum.ErrorKind.html https://docs.rs/regex-syntax/0.4.0/regex_syntax/enum.ErrorKi... Now, in most cases, callers don't care at all about which specific error happened. They just want to print the error and be done with it. And that works just fine, because of the error handling machinery. But if you do want to drill down, then the option is there for you.
- leshow 9y agoYou can use pre defined error types. Have you seen https://doc.rust-lang.org/std/io/enum.ErrorKind.html https://doc.rust-lang.org/std/io/enum.ErrorKind.html ?
- rspeer 9y agoThat seems to just be kinds of IO errors, though.