5 ms·
These struggles are indeed real, but at least as far as verbose error handling goes, remember that Rust is forcing you to handle a lot of things that are silent
by rvense 10y ago
These struggles are indeed real, but at least as far as verbose error handling goes, remember that Rust is forcing you to handle a lot of things that are silently ignored in Python. Truly equivalent Python code would include a bunch of exception handling and checks for nil.
- nathan_f77 10y agoI haven't done much Rust, but why isn't there a safe way to do some of this implicitly? If you don't want to think about errors and just want to crash, then I feel like there should be a sane way to crash the script with a default error message. Could you write a thin abstraction to achieve this? Maybe there could be a new crate called "rust-script" or something, where the goal is to write code as easily as Python or Ruby. Again, I'm not a Rust developer, but it's not hard to imagine an abstraction (or even a transpiler) that makes it easy to read a file, parse it as JSON, and do something with the data.
- AsyncAwait 9y agoThere's unwrap() and panic!, does that not do what you want?
- bombless 9y agoYour answer is simply `unwrap`. OP know it but struggles to believe it, it's that simply.
- rvense 9y agoAs the other comment says, that's exactly what's done with unwrap and try. You do obviously have to write that out, but on the other hand that means you know where to add the error handling when you come back after the fact. It might take a little getting used to, but it's a decent compromise. I'd say it's a matter of taste, but yeah, there are a few more characters there.
- MichaelGG 9y agoCouldn't there be an opt-in Deref for Option and Result? Then you could just pretend everything unwrapped fine.
- leshow 9y ago> If you don't want to think about errors and just want to crash, then I feel like there should be a sane way to crash the script with a default error message. We have `expect` it's like unwrap(), but takes a string. When the program encounters some error, it will exit and print the string.
- cowsandmilk 10y agoThe author seems to want to not use unwrap() in the rust version when that is basically what is done in the python version. The python version will die with an exception if: 1) the file does not exist 2) the file is not readable 3) the file cannot be parsed Rust forces you to say you want to panic in these cases (by using unwrap), but beyond that, the behavior is similar.