4 ms·
It's an empty tuple stuffed into an enum variant called Ok. The empty tuple plays the role of the traditional void.
by nemaar 10y ago
It's an empty tuple stuffed into an enum variant called Ok. The empty tuple plays the role of the traditional void.
- dikaiosune 10y agoImportantly, it's the Ok variant of the Result<(), SomeError> type, which is richer than a boolean because it carries information about why/where an operation failed, not just whether it succeeded or failed. It's similar to (but not identical to): public void someOperation() throws SomeException { ... }
- cfallin 10y agoI like to compare it to the "comma error" idiom in Go, i.e., `result, err = doStuff(...)`. The Rust prelude's Result enum is a less ad-hoc version of that.
- dikaiosune 10y agoRight, and the Rust result also produces a compile-time warning (which can be escalated to an error) if the value isn't checked.
- yarper 10y agoIt supports that kind of error handling too, fn something() -> (String, String) ... let (result, err) = something() It's just that Result is preferable because you can pattern match on it
- kazagistar 10y agoThat would be fn something() -> (Option<String>, Option<String>) Because the Rust types aren't nullable by default. But that more clearly demonstrates that Result types are better since they enforce that the error and result are mutually exclusive.
- yarper 10y agoagreed!