by adwn · 1w ago · 0 comments
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