Hacker News new | ask | show | jobs
by adwn 13 days ago
Applying the ? operator to an Option<_> doesn't work inside a function returning io::Result<_>, for example, and turning the None from an out-of-bounds vector access into some io::Result::Err usually wouldn't make sense.

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.

1 comments

The former issue arises from post-hoc thinking. Of course it's hard to retrofit into the language, but I see no reason why there shouldn't be facilities for the ? operator to turn a None into an appropriate error type.

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.

> no reason why there shouldn't be facilities for the ? operator to turn a None into an appropriate error type

There are facilities for this.

   option.ok_or(MyError::new())?
> do more sophisticated stuff (core dump, cleanup or notifications)

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