4 ms·
Is it that verbose, though? I have a hard time seeing how it could be made any shorter in other languages, without sacrificing functionality. EDIT: I think wha
by MHordecki 10y ago
Is it that verbose, though? I have a hard time seeing how it could be made any shorter in other languages, without sacrificing functionality.
EDIT: I think what gives the code its length here is the explicit error handling. If you do the same in Python you'll end up with the same amount of code. In Rust, you could unwrap() everything and lose nice error messages and probably slash the LOC in half.
- reissbaker 10y agoWith 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.