Live data from Hacker News

Using unwrap() in Rust is Okay (2022)

burntsushi.net

51–54 of 54 posts

Re: Using unwrap() in Rust is Okay (2022)

#51
post #31
post #8

The article pretty much says this as well, but a concise way I saw Andrew Kelley put it recently in the context of Zig (but it seems to apply well for any language that has errors-as-values + panics) is: "Assertions for programmer mistakes; errors for user mistakes."[0] One interesting difference between Zig and other languages with similar error stories (Rust, Go) is that panicking can be handled but not recovered .…

From my experience, Zig is the worst at error handling. Not that the language itself make it hard, on the contrary, but the standard library itself is littered with opinionated error handling that straight up panic with no recovery at every corner. For instance, it was decided that the kernel returning EINVAL for any syscall is worth panicking, even though there are a lot of cases where that is recoverable. The answe…

Quotes are cute, and I prefer them over uncited negative claims. Can you link somewhere he actually said that? Because from a quick search I find him saying:

> It should be handled on a case-by-case basis. For some syscalls, EINVAL should be unreachable because it can only mean something like an invalid pointer address being passed to the kernel, or invalid flags.

> However, as you pointed out, some kernel APIs unfortunately have decided to dual-purpose this error code to mean other things, in which case we must to handle the ambiguity by mapping it to an error code. We had to make this change with EBADF recently for some syscalls, for example.

> I do want to keep the error sets clean, however, so I am opposed to a blanket modification of all EINVAL code sites without cause.

Source: https://github.com/ziglang/zig/issues/6925#issuecomment-7214...

Re: Using unwrap() in Rust is Okay (2022)

#52
post #51
post #31

Earlier quoted context omitted.

From my experience, Zig is the worst at error handling. Not that the language itself make it hard, on the contrary, but the standard library itself is littered with opinionated error handling that straight up panic with no recovery at every corner. For instance, it was decided that the kernel returning EINVAL for any syscall is worth panicking, even though there are a lot of cases where that is recoverable. The answe…

Quotes are cute, and I prefer them over uncited negative claims. Can you link somewhere he actually said that? Because from a quick search I find him saying: > It should be handled on a case-by-case basis. For some syscalls, EINVAL should be unreachable because it can only mean something like an invalid pointer address being passed to the kernel, or invalid flags. > However, as you pointed out, some kernel APIs unfor…

> We discussed this in the meeting today and decided that we'll start by changing all => unreachable to => return error.Unexpected in std.posix and std.os.

https://github.com/ziglang/zig/issues/6389#issuecomment-2895...

Re: Using unwrap() in Rust is Okay (2022)

#53
post #49

Earlier quoted context omitted.

I suppose it's a matter of perspective but I don't see returning a 500 as "Recovering from the error". The user's request has still failed. It just hasn't taken down the server. IMO this is still fine.

It is quite literally using the "recover" function in go, so I think it's accurate to call it "recovering". > this is fine I didn't mention the merit, just that it is exception style control flow.

Reading my top level comment again, I realized I did refer to the Zig way as "nice" relative to the exception style control flow.

Re: Using unwrap() in Rust is Okay (2022)

#54
post #16

Earlier quoted context omitted.

> It doesn't look to be too much of a temptation to use panics as regular errors in Rust though Maybe not, but it was enough for the author to dedicate at least one whole section of the article to reasoning about this. > like when you want to show an error box to the user, instead of silently crashing, or when you want to send the crash log for later analysis As I mentioned, panics can be handled but not recovered .…

> As I mentioned, panics can be handled but not recovered. You can use the panic handler to do these things just fine. Just not recover. What does this difference mean? If you can run arbitrary function in your panic handler, what prevents someone from continuing the program?

The stack is fully unwound before running the panic function, including the main stack frame. This will run all deferred deallocation statements, and pretty much leave you with nothing to do but exit. You could try to launch the program again automatically, but there is little reason to do that most likely. The user will still see the application terminate and launch again.
Post reply on HN