3 ms·
Let me introduce you to the concept of ErrorKind in Rust (i.e. in the IO module of the std): https://doc.rust-lang.org/std/io/enum.ErrorKind.html https://doc.ru
by Lev1a 8y ago
Let me introduce you to the concept of ErrorKind in Rust (i.e. in the IO module of the std): https://doc.rust-lang.org/std/io/enum.ErrorKind.html https://doc.rust-lang.org/std/io/enum.ErrorKind.html
There is also the "failure" crate which has patterns for the kind of thing you mentioned:
https://boats.gitlab.io/failure/error-errorkind.html https://boats.gitlab.io/failure/error-errorkind.html
https://boats.gitlab.io/failure/custom-fail.html https://boats.gitlab.io/failure/custom-fail.html
- gmfawcett 8y agoI don't believe that either ErrorKind or 'failure' gives the same degree of automated enumeration and checking that Zig provides. I'm not a Zig apologist... I just think you should read through the Zig documentation and examine the differences for yourself. https://ziglang.org/documentation/master/#Errors https://ziglang.org/documentation/master/#Errors https://ziglang.org/download/0.2.0/release-notes.html https://ziglang.org/download/0.2.0/release-notes.html (see "Error Sets")
- deleted 8y ago[deleted]
- bpicolo 8y agoIf I'm understanding correctly, you can use enums in rust to recreate the same, though it would be slightly more verbose. enum MyErrorSet { NotFound(ErrorKind), SomethingElse(ErrorKind), } If you don't care about propagating the original ErrorKind then that's just enum MyErrorSet { NotFound, SomethingElse }
- gmfawcett 8y agoYes, I think you're right. But the burden for generating and maintaining the enums is on the developer; whereas in Zig, they can be automatically inferred by the compiler. I don't think the distinction is super important in application work (for example). But for systems code I can see the benefit of having the compiler tell you, with certainty, why a given function can fail (and why it can't). E.g., a good device driver should handle all its failure modes with care, rather than with a shotgun approach.