by cube00 · 3w ago · 0 comments
The ? operator can be nicer to read then all those .unwraps() as an alternative and doesn't panic.
I guess the concern with panics is you're not forced to think about how you want to handle them like you do when you get a `Result` or `Option` returned.
As a result it could be used as a DDOS against your app if it's network accessible.
I would go so far as to say that an out-of-bounds index is always a logic error (to be handled by panicking), not an input error (to be handled with Result). And if the out-of-bounds access was caused by invalid input, then logic error consists of not checking the input before indexing.
The latter is a subjective matter. There are reasons to never want a panic (hello there, Linux kernel) and reasons to do more sophisticated stuff (core dump, cleanup or notifications) before crashing. And maybe one thread shouldn't lead to process death. I think most of these decisions should be kept to the user's discretion.
There are facilities for this.
option.ok_or(MyError::new())?That's what panic::set_hook is for. [1]
> And maybe one thread shouldn't lead to process death.
That's what panic::catch_unwind is for. [2]
[1] https://doc.rust-lang.org/std/panic/fn.set_hook.html
[2] https://doc.rust-lang.org/std/panic/fn.catch_unwind.html