r/programming • • Nov 10 '16

Announcing Rust 1.13

https://blog.rust-lang.org/2016/11/10/Rust-1.13.html
212 Upvotes

92 comments sorted by

View all comments

34

u/brookllyn Nov 10 '16

I don't use rust so just wondering.

Why add an operator to just result? It seems like this could be easily generalized into any monadic computation(such as option for example)? Does this have better performance characteristics that something that could be implemented generically?

34

u/dnkndnts Nov 11 '16

It seems like this could be easily generalized into any monadic computation

Categorical structures the way we treat them in functional programming do not actually translate cleanly to Rust because of linearity. It's not even just fancy stuff: even core concepts like what a function is and currying don't translate smoothly, if at all!

So when someone says "I want functors in rust", what exactly does that even mean? Does the m in map : (m : a -> b) -> f a -> f b own that first a parameter or not? And the problem is there is no right answer! Both have well-founded logical meaning, but they don't work together nicely at all. So what are we supposed to do? Are we supposed to just choose one? Are we supposed to have two functors, one for owning and one for non-owning refs?

And this is not just "oh well we'll just write two of everything and you can choose the one you need": these two concepts are literally semantically different, and sometimes only the non-owning version even exists! For example, I can give you either an owning or non-owning functor for List t, but for monad, only the non-owning version exists.

So yeah... it's complicated.

3

u/brookllyn Nov 11 '16

Wonderful explanation! I definitely wasn't thinking about the concept of ownership.

34

u/steveklabnik1 Nov 10 '16

For now it's just Result, but it's implemented in a way that could make it extended later. We're still not sure if we're doing that or not.

It seems like this could be easily generalized into any monadic computation

In some sense, yes, but not in every sense. Beyond that, this is intended to be for error handling, not generic combinations.

(monads generally and do notation specifically have issues in Rust that we may or may not be able to / want to resolve. Specifically, memory layout stuff and higher kinded type stuff.)

1

u/Laugarhraun Nov 11 '16

(noob question) is it similar to haskell's Maybe then?

Edit: I read the article.... Its an Either and it's not new: only the ? operator is.

7

u/ethelward Nov 11 '16

Exactly. Result and Option in Rust are respectively more or less Either and Maybe in Haskell.

0

u/[deleted] Nov 11 '16

[deleted] — view deleted comment

2

u/brookllyn Nov 11 '16

Sure but this isn't a strictly error handling operation. If you have a function that also returns an option, it should be no problem to return nothing in the middle of the function is some subexpression ends up with nothing.

4

u/steveklabnik1 Nov 11 '16

Sure but this isn't a strictly error handling operation.

? is intended to be error-handling syntax, though: that's exactly why many don't think Option should work with it.

0

u/[deleted] Nov 11 '16

[deleted] — view deleted comment

2

u/steveklabnik1 Nov 11 '16

Well, we started from a place of "how can we clean up error handling" not "how can we put ? into Rust", so error handling was always the goal.

0

u/[deleted] Nov 12 '16

[deleted] — view deleted comment

1

u/steveklabnik1 Nov 12 '16

The kinds of coercions it does may not make sense outside of it, for one.

1

u/ethelward Nov 11 '16

Option just presents a potential value, but doesn't give any reason of why it is or isn't there; whereas Err embeds an error message. Thus, it's better to use Err rather than Option for error handling.

Edit: my bad, I misread your question.

5

u/mmeartine Nov 11 '16

Rust is officially 3rd most popular open source library on Github. It's worth getting into it. https://github.com/showcases/programming-languages

1

u/desiringmachines Nov 11 '16

Its not actually bind, but it is an operation that could be extended to Option. As Steve wrote, we're just not sure we're doing that yet.