4 ms·
I found the python version of the code so much more readable... is putting "unwrap" and "ignore" everywhere becoming idiomatic in Rust ?
by bsaul 12y ago
I found the python version of the code so much more readable... is putting "unwrap" and "ignore" everywhere becoming idiomatic in Rust ?
- Argorak 12y agoUsually, .unwrap_or_else(|e| { fail!("well, that's unexpected") }) You need to handle all results in Rust (because the second option is the error case), or the linter will complain. Considering that failing the task (like in e.g. Erlang) is the standard way to handle errors, yes, this is somewhat idiomatic.
- dbaupp 12y agoFailing the task isn't the standard way to handle errors; propagating information via the Result type (especially the try! macro) is more conventional, as it allows the caller to handle the problem, failing is very annoying to recover from. - http://doc.rust-lang.org/nightly/std/result/ http://doc.rust-lang.org/nightly/std/result/ - http://doc.rust-lang.org/nightly/std/result/#the-try!-macro http://doc.rust-lang.org/nightly/std/result/#the-try!-macro
- Argorak 12y agoI didn't make that clear enough: I should have qualified by "if the error is indeed fatal at that point". In the example script, it really doesn't make sense to go on after that.
- yen223 12y agoPython made the choice to sacrifice performance for much better readability, and it shows. You can see the difference between code elegance when working with strings in either languages, for example. The downside is that Python's benchmark numbers are poor. Rust can get to within 1x C's performance; Python would be lucky to get within 100x. But I do share your concern about `unwrap`. Is it idiomatic to do `unwrap`s, or is it better to define functions to accept and return Option<xxx>?
- the_mitsuhiko 12y agoUnwrap only appears in simplistic examples. The correct "unwrap" is the `try!` macro.
- FreeFull 12y agoOnce you reach the main function, you have to deal with the errors, since you can't propagate them any further.
- the_mitsuhiko 12y agoYour main function is most likely pretty empty and that's the only place where you need to deal with hard failure. And that's the same in all languages. If you write in Python you don't just not handle exceptions either. Somewhere you catch them down and present them to the user in a good way.
- pnathan 12y agoPerformance and readability are not tradeoffs.
- the_mitsuhiko 12y agoNo, it's just in this example. In idomatic rust you would write this probably: https://gist.github.com/mitsuhiko/1f37c1092bdf54ce4213 https://gist.github.com/mitsuhiko/1f37c1092bdf54ce4213 The `try!` macro unwraps the okay part of the expression and early returns the err part. The nice thing about the explicit results is that you will do the error handling. With Python you often do not know if an error might come.
- bsaul 12y agoOk, much better. At the risk of looking more like a java try/catch, is it possible to wrap multiple statements in a single try! clause ? Or at least have some way of saying " if there's any error in this block of code, simply stop execution and forward it to the calling statement"
- the_mitsuhiko 12y agoNo, but that's would also defeat the purpose. The idea of results is that you know from looking at the code what can fail. If you remove that information you might as well use exceptions.