
If we have two different types of errors, we have to get creative. The problem is though, that methods like this only work when the error types are the same. The question mark operator also works on Option, this also allows for some really nice and elegant code! Propagating different errors # Errors still have to be dealt with! Either from us or from the users of our function.
We can write concise code as if errors wouldn’t exist. We are still explicit, we have to do something! You can still find all the spots where errors can happen!. Result is an enum with two variants: enum Result ", err ) In this article, I want to focus on Result as this one actually contains errors. Either by explicitly handling all states, or by explicitly ignoring them. The most important thing, in my opinion, is that both types request from you to deal with them. The way both enums work is very similar though. The difference between the two is very nuanced and depends a lot on the semantics of your code. Result for results from operations that might error (e.g. It offers core types, like Vec and Option, library-defined operations on language primitives, standard macros, I/O and multithreading, among many other things.
Downcast rust portable#
Option for bindings that might possibly have no value (e.g. The Rust Standard Library is the foundation of portable Rust software, a set of minimal and battle-tested shared abstractions for the broader Rust ecosystem.
Instead, you use built-in enums to model state: In Rust, there are no things like undefined or null, nor do you have exceptions like you know it from programming languages like Java or C#. Not only does it help making impossible states impossible, but there’s also so much detail to it that handling errors – much like everything in Rust – becomes very ergonomic and easy to read and use. One of the parts that evolved from a couple of slides into a full-blown session was everything around error handling in Rust, since it’s so incredibly good! I started doing university lectures on Rust, as well as holding workshops and trainings.