Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> This is the multi-million dollar .unwrap() story.

While there are certainly many things to admire about Rust, this is why I prefer Golang's "noisy" error handling. In golang that would be either:

    feature_values, err := features.append_with_names(...)
And the compiler would have complained that this value of `err` was unused; or you'd write:

    feature_values, _ := features.append_with_names(...)
And it would be far more obvious that an error message is being ignored.

(Renaming `unwrap` to `unwrapOrPanic` would probably help too.)



But I could screw it up in Go, if I made the same assumptions

    fvs, err := features.AppendWithNames(..)
    if err != nil {
        // this will NEVER break
        panic(err)
    }
Ultimately I don't think language design can be the sole line of defence against system failures; it can only guide developers to think about error cases


Right, but the point isn't to make errors impossible; the point is to have them be 1) less likely to write, and 2) easier to spot on review.

People's biggest complaints about golang's errors:

1. You have to _TYPE_OUT_ what to do on EVERY.SINGLE.ERROR. SOO BOORING!

2. They clutter up the code and make it look ugly.

Rust is so much cleaner and more convenient (they say)! Just add ?, or .unwrap()!

Well, with ".unwrap()", you can type it fast enough that you're on to the next problem before it occurs to your brain to think about what to do if there is an error. Whereas, in golang, by the time you type in, "if err != nil {", you've broken the flow enough that now you're much more likely to be thinking, "Hmm, could this ever fail? What should we do if it does?" That break in flow is annoying, but necessary.

And ".unwrap()" looks so unassuming, it's easy to overlook on review; that "panic()" looks a lot more dangerous, and again, would be more likely to trigger a reviewer into thinking, "Wait, is it OK if this thing panics? Is this really so unlikely to happen?"

Renaming it `.unwrap_or_panic()` would probably help with both.


Eh, I'm not convinced.

1. Culturally, using `unwrap` is an omerta to Rust developers in the same way `panic` is an omerta to Go devs;

2. In the Rust projects I've seen there is usually a linter rule forbidding `unwrap` so you can't use it in production


> omerta

Unfortunately none of the meanings Wikipedia knows [1] seems to fit this usage. Did you perhaps mean "taboo"?

I disagree that "unwrap()" seems as scary as "panic()", but I will certainly agree to sibling commenters have a point when they say that "bar, _ := foo()" is a lot less scary than "unwrap()".

[1] https://en.wikipedia.org/wiki/Omerta_(disambiguation)


That may be me, but `.unwrap()` is much more obvious than `_`:

- it's literally written out that you're assuming it to be Ok

- there are no indications that the `_` is an error: it could very well be some other return value from the function. in your example, it could be the number of appended features, etc

That's why Go's error handling is indeed noisy: it's noise and you reduce noise by not handling errors. Rust's is terse yet verbose: if you add stuff it's because you're doing something wrong. You explicitly spelled out the error is being ignored.


> And it would be far more obvious that an error message is being ignored.

Haven't used Go so maybe I'm missing some consideration, but I don't see how ", _" is more obvious than ".unwrap()". If anything it seems less clear, since you need to check/know the function's signature to see that it's an error being ignored (wouldn't be the case for a function like https://pkg.go.dev/math#Modf).


I haven't been writing Rust for that long (about 2 years) but every time I see .unwrap() I read it as 'panic in production'. Clippy needs to have harder checks on unwrap.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: