2 ms·
With the built-in error handling macro, you can generally do: let value = try!(fn_that_may_error()); Rather than having to muck around with explicit match
by reissbaker 10y ago
With the built-in error handling macro, you can generally do:
let value = try!(fn_that_may_error());
Rather than having to muck around with explicit matching on Ok vs Err.
The try! macro would make the linked function much less verbose... Except that what try! does is bubble the error further up the stack by returning an Err if the called function returns an Err — and since this is the main function, there's nowhere up the stack to go (main() has a return type of "Unit," which is Rust's version of void or null; for functions that pass errors up the stack you'd have a return type of Result<ReturnType, ErrorType>, which allows errors to be returned), so try! can't work.
I agree that some of that error handling in 79-101 is pretty awkward looking, but luckily it's not super representative of idiomatic Rust: usually you can make Rust much more concise by using try!, with the exception of the main() function which can't use try! and has to actually handle the error.
- moosingin3space 10y agoI'd prefer to use `expect` in this case, or if I wanted to present a non-scary error message, write a custom macro to print the error and exit.