Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Idiomatic Rust uses try!. It is a less verbose version of the most common pattern of error handling in Go. You have not programmed very much Rust and are not yet familiar with its idioms, but that is by far the most common and avoids all the problems you just described.


"try!" is close to something one might call "cruft". It tests a function for an error value, and if it gets one, it returns from the containing function. "try!()" is written like a function call, but it doesn't work like one - it can execute a return statement. That's outside the normal semantics of the language. This is the only macro in the standard macros with such semantics. Fortunately. This is a capability which needs to be used with extreme restraint. Writing a macro "try_except_finally!(maincode, exceptioncode, cleanupcode)" would probably be a bad idea.

There was at one time a fad for extending C with macros like that. Fortunately for program readability, it didn't last.


`throw` in Java's exception handling also prematurely exits a function. So?

While `try!()` is a macro and not a core language feature, most of the macros in the standard library can be treated as such. So a Rust programmer will know that `try!()` can return, just like a Java programmer knows that `throw` returns early.

Besides, macros are syntactically distinguishable in Rust. When you see that exclamation mark, you have to rememeber that it's a macro and might be doing arbitrary compile time things along with arbitrary token tree expansion.


Except that in Rust, all macros are syntactically distinguished from non-macros (as well as hygienic, unlike C macros), and try! is extremely common (so the claim that people won't know what it does is questionable). I agree that macros like this should be carefully considered, but I think experience has already shown us that try! is perfectly fine.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: