3 ms·
With the try! macro, it's got the test and return built in. Further, if the return value is large, the caller will allocate space for it and pass the address of
by barrkel 10y ago
With the try! macro, it's got the test and return built in. Further, if the return value is large, the caller will allocate space for it and pass the address of the space as a hidden parameter. If you've got rich return values, the final code will be pretty much isomorphic with the example C code (it wouldn't be a pointer to a pointer, though; it would be a pointer to a struct).
- danieldk 10y agoNote that Rust now also has the ? operator, which can be a bit easier on the eyes than try!: https://github.com/rust-lang/rfcs/blob/master/text/0243-trait-based-exception-handling.md https://github.com/rust-lang/rfcs/blob/master/text/0243-trai... https://blog.rust-lang.org/2016/11/10/Rust-1.13.html https://blog.rust-lang.org/2016/11/10/Rust-1.13.html
- saurik 10y agoFWIW, C++ has this really cool feature they call "exceptions", which is a language-level implementation of Result, try!, and ? that is automatically included as part of any function that would otherwise have to manually unwrap and pass along an error in languages like Rust; a really great bonus is that because the compiler has implemented this feature and abstracted it from the developer, in addition to it removing a lot of boilerplate from your code, it allows for an extremely fast table-based implementation they call "zero-cost exceptions" that can almost always completely eliminate the CPU cost of having to constantly check and forward error information.
- kaoD 10y ago> really cool feature Arguable.
- izym 10y agoYes, people are quite aware of that, and if you check the comments you'll see points made against it. Also, ? is very much language level.
- kibwen 10y agoCondescending sarcasm is unwarranted. People are well aware of what exceptions are, and the arguments against them amount to more than just performance.
- sidlls 10y agoI've implemented a Result-like error code construct in plenty of C++ projects and disabled exceptions precisely because I don't think they're a good way of handling error states in code.