3 ms·
I haven't done much in Rust yet, but it seems you can either use `Result` to handle errors (and force the caller to deal with it) or crash with `panic`. I wonde
by captainmuon 6y ago
I haven't done much in Rust yet, but it seems you can either use `Result` to handle errors (and force the caller to deal with it) or crash with `panic`. I wonder, is there any way to specify "Crash, unless someone up the call stack deals with this?". What I usually do in other languages is use exceptions as assertions, when it is just a simple one-off app, but then go back and catch the exceptions when possible to make it more user friendly and robust. It seems that exceptions have been omitted on purpose, but is there another way to express this idiom in Rust?
- masklinn 6y ago> I haven't done much in Rust yet, but it seems you can either use `Result` to handle errors (and force the caller to deal with it) or crash with `panic`. I wonder, is there any way to specify "Crash, unless someone up the call stack deals with this?" It's possible to catch panics during unwinding[0], but that will not work in every case: `panic='abort'` is a completely valid configuration for a Rust compilation. > What I usually do in other languages is use exceptions as assertions Rust assertion macros will panic. `unwrap` methods are also, functionally, assertions. And they panic. > when it is just a simple one-off app, but then go back and catch the exceptions when possible to make it more user friendly and robust. It seems that exceptions have been omitted on purpose, but is there another way to express this idiom in Rust? That is very much not idiomatic rust[1] and I would recommend against it, but it is possible for applications as you'd hopefully be controlling the compilation configuration. For libraries however it's a good way to shoot everyone in the foot: you simply can not assume that users of your library will `panic='unwind'`, so it is a very dangerous thing to do. [0] https://doc.rust-lang.org/std/panic/fn.catch_unwind.html https://doc.rust-lang.org/std/panic/fn.catch_unwind.html [1] https://doc.rust-lang.org/book/ch09-03-to-panic-or-not-to-panic.html https://doc.rust-lang.org/book/ch09-03-to-panic-or-not-to-pa...
- imron 6y ago> but is there another way to express this idiom in Rust? If you don’t want to handle Results, just ? all the way up and out of main. Then come back and deal with them as necessary when you’re ready. It’s far nicer than dealing with exceptions.
- masklinn 6y agoAlso `anyhow` exists specifically to support that use-case: you're developing an application and you largely just want to bubble up errors and show them to the user, with the odd spot of internal handling.
- danieldk 6y agobut it seems you can either use `Result` to handle errors (and force the caller to deal with it) or crash with `panic` To me, there is a large difference between them. Result is for computations that can fail, such as opening a file or reading from a socket. Computations that fail should be handled by the caller (e.g. report a nice error which file could not be opened or attempting to open a new socket and retry the communication). panic, in particular through assertions, are for catching programming errors, such as violation of invariants. These are cases where the program should just crash and burn, because it would be in an invalid state otherwise. E.g. splitting a Vec at an invalid index is a programming error. If a program attempts such a split, it is already in an invalid state, so it should crash and the bug should be fixed. What I usually do in other languages is use exceptions as assertions, when it is just a simple one-off app, but then go back and catch the exceptions when possible to make it more user friendly and robust. The Rust equivalent is letting Result::Err bubble up the call chain and have a fn main() -> Result<(), Error> or even nicer: fn main() -> anyhow::Result<()>