DEV Community

Cover image for Two Rust features that I miss in Other languages

Two Rust features that I miss in Other languages

YJDoc2 on April 13, 2023

Hello everyone! Now-a-days I am writing code in several different languages : Rust, C , JavaScript, Python. While all of these have their own use ...
Collapse
 
nsengupta profile image
Nirmalya Sengupta

Good article.

#[must_use] is a great aid. I agree with you.

However, we can use an exact equivalent of Result in Java/Scala. It is called 'Either'. Perhaps, you already know about it. When instantiated, a value of Either can be of either the 'L' type - conventionally, indicating an error - or the 'R' type, which is the value, we are otherwise looking for.

Scala has built in Either type. In Java 8+ (I have used till Java 17), by using the fantastic 'vavr' library, 'Either' can be put to use, including the pattern-matching idioms, map(), flatMap() etc, much like Rust lets us.

I don't know Kotlin, but I assume Kotlin has something similar. Experts can comment.

Collapse
 
yjdoc2 profile image
YJDoc2

Hey, thanks for the comment! I have updated the article to mention this. I don't work much with Java/Scala so didn't know that. Does the Either type also have similar ergonomics as rust's Result? I know that types like Rust's enum (I think they are called additive data types?) exist in several other languages, but I didn't knew those have similar ergonomics as Rust's results have.

Collapse
 
nsengupta profile image
Nirmalya Sengupta

As for ergonomics, @omidrad has already elaborated. the difference between Rust and Java. I differ slightly from his PoV and I have stated that in my response to him.

Collapse
 
swandog profile image
Ken Swanson

Scala also has the Try type, which is the more exact analogue to a Rust Result, and it's had it since 2.10.

Some older Scala code uses Either since it came out first, but most of the Scala code I've read and written (admittedly, it's been a few years) use Try[T] over Either[T, Error].

Collapse
 
omidrad profile image
Omid Rad

But that's different.

When you are in the ecosystem and use some dependencies, some deps use Either and some use exception. It makes things more complicated and ugly and at some point you need to handle both at the same time!

Either does exist in Rust (as library, and removed long time ago from the std lib), but IMO to use it as a way to handle errors is lame.

On top of these, there is no difference between L and R in Either, there is no Ok and Err. A library may see L as Err and another one R as Err. Also what about ? in Rust?

Anyway, they are not the same.

Collapse
 
nsengupta profile image
Nirmalya Sengupta • Edited

Not same, of course. But equivalent. :-)

I have steadfastly - one may say determinedly - stayed away from all checked exceptions for last few years in all my Java applications. Wherever pressed, I have wrapped the exception-throwing methods into Either returning methods.

Not easy always, as you have correctly pointed out - but demonstrably doable.

The other point you raise about L and R being not a fixed type, you are right. The convention of course, is that L is for errors and R is for acceptable values. However, nothing stops me from making L a subtype of an master Error type. The compiler does the rest.

Thread Thread
 
omidrad profile image
Omid Rad

I would say they are not equivalent also.

That's a misuse of Either to handle errors on other languages. Or let's say the best way they can do so. There are a lot around Result in Rust. There is nothing specific for error handling for Either.

The only common thing is that, both Result and Either are two-variant enums! (I don't know how exactly they called them in Java)

It's like people saying Java also has Option. But that's different between when the whole ecosystem only has Option or there is a possibility of using Options. They are not the same or equivalent.

We don't have these kinds of inconsistency, because it's a younger language. But I hope we don't have it in future also.

Thread Thread
 
swandog profile image
Ken Swanson

Scala Eithers are not exactly as blank as that, because they are "right-biased"; meaning that, in all comprehensive operations, it considers Right values to be present and Left values to not be. This is why you can chain flatMaps of Eithers together without having to designate what the types mean; for a Scala Either, the Right is always the default.

It's also why an Either with an error in the Left type is a simple implementation of the Result type; you map/flatMap over them, and if you end up with a Left, you got an error.

That said, as I said elsewhere Scala also has the Try type which I think works better, if for no other reason than the semantics around it more clearly indicate you're dealing with errors.

(Scala doesn't have the ? operator, it's true, because IIRC the style prefers keeping values inside the container and applying transformations to it, instead of extracting the value or returning early. YMMV)

Thread Thread
 
omidrad profile image
Omid Rad • Edited

Scala doesn't have anything related to errors for its Either. Does it have is_err for example (sure you can check if the left is there or right)? Does it have map_err? Does it have expect? Does it have or_X functions? Does it have unwrap functions? and so on...

There can be a library which can add these functionalities to the language. But as I mentioned many times, it's not the same!
When you can use different ways of having errors (in this case) and you don't follow the language standard/default way. It creates inconsistency in the ecosystem, specially as a library developer or when you use external libraries!

If I was a Java/Scala developer, and wanted to develop a library, I would never use Either to handle/expose errors.

But Try is something similar to Rust's Result. The only bad part is that now devs need to handle Either, Try and Exceptions (and maybe more).

Thread Thread
 
nsengupta profile image
Nirmalya Sengupta

I am not into splitting hairs on semantic niceties! :-)

A concept exists in Scala / Java ( In Elixir and Haskell too, my FP guru friends tell me), to indicate that a function can return either of two possibilities but never, both.

If a function can return two different types of SUCCESS indicators, and one decides to use Either for that, it is fine! But, that's not what the common sense dictates.

The equivalence that I pointed out was about a facility in the type system to disassociate an ERROR from an ACCEPTABLE value that a function returns: much more easier to deal with, say errno of my C days or random Exceptions thrown in a JVM-based code. That's about it.

You are right, Rust has the advantage of being younger than many other languages and is offering a cleaner way to disassociate ERROR values from ACCEPTABLE values. Compiler's watchful eyes point out any inconsistencies earlier in the cycle. That helps a lot, too.

Thread Thread
 
nsengupta profile image
Nirmalya Sengupta • Edited

It's also why an Either with an error in the Left type is a simple implementation of the Result type; you map/flatMap over them, and if you end up with a Left, you got an error.

My point too.

And, there is a reason why Try in Scala / Java ( vavr gives the equivalent Try type in Java), has methods like toEither() and toOption().

Collapse
 
val_baca profile image
Valentin Baca

I haven't tried it, but it looks like Arrow is the Kotlin FP batteries included library and has Either (Kotlin can also just use vavr afaik)

arrow-kt.io/learn/typed-errors/eit...

Collapse
 
pkraszewski profile image
Paweł Kraszewski

there is no straightforward way of doing something similar in other languages.

With all my hate to if err!=nil{}, Go has error wrapping:

var criticalError = errors.New("Serious error")
// ...
wrapped := fmt.Errorf("...%w...",criticalError,...)
// ...
parent := errors.Unwrap(wrapped)

Enter fullscreen mode Exit fullscreen mode

See rollbar.com/blog/golang-wrap-and-u...

Collapse
 
yjdoc2 profile image
YJDoc2

Hey, I have updated the article to mention this. I had only seen the err != nil kind of error handling, and haven;t worked with go much, so didn't know that.

I checked the link, but I'm not sure. Does the wrapping-unwrapping provide similar ergonomics as Rust Results too?

Collapse
 
pkraszewski profile image
Paweł Kraszewski • Edited

Does the wrapping-unwrapping provide similar ergonomics as Rust Results too?

Hell not. I hate it not without a reason. I still find Rust's error handling superior to all the other languages I use(d).

Collapse
 
omidrad profile image
Omid Rad

Also when you are in the ecosystem and use some dependencies, some deps use unwrapped version and some use wrapped version. It makes things more complicated!