Rust – Coroutines support merged
github.com
github.com
> The key point then is that the experimental RFC is not enough to stabilize something. For that, we need a proper RFC that lays out the plan. This RFC can be informed by the experience and working implementation, so we should have a lot of data to use.
It's being merged into nightly (behind a flag) to gain understanding of how the feature would work. This isn't the day to celebrate Rust having fully-fledged co-routines coming soon to stable because there is no such guarantee yet.
Edit: removed extra word
This divides the language ecosystem and makes it hard for library writers to support both concurrency models.
Ask anyone who has done significant async/await coding in C#/Python. It gets messy fast.
Async/Await works a little better in JS because it always has had a cooperative concurrency model, standard library functions usually don't block (except sometimes in Node). Though it's still annoying to have to check whether a function returns a promise or not.
Languages that do this right are Haskell and Go and probably Elixer/Erlang. This usually requires eschewing LibC (which usually assumes a threaded concurrency model) and writing your own runtime.
> writing your own runtime.
This is not a cost that Rust can bear, and so that option is just not possible. Unfortunately, there's no such thing as a free lunch.
The RFC that made this change https://github.com/rust-lang/rfcs/blob/master/text/0230-remo...
Especially, indirect function calls for IO functions seem fine since IO is usually much slower than a function call. Also binary size increase may not be a problem for people writing web-scale servers.
It would be great if the Rust compiler infrastructure provided the necessary hooks for re-implementing std::/using a different concurrency model and independent projects could make those "problems" trade-off decisions on a individual basis. Rust-core could just only support/ship the native threaded model to lower maintenance burden for themselves.
It was a big enough problem that the language was almost forked over it.
> It would be great if
Rust is low-level enough that you can do this, and some people have. But then people have to use your package. std is only special in that it can use unstable code but be part of stable Rust; otherwise, it's just regular old Rust code. The community overall abandoned all of those other things and is coalescing around Tokio, which could also be considered this, in a sense. Or Rayon, if you want data parallelism rather than async IO.
One of the worst parts of async is there's an infectious duplication of pretty much all library code where there are separate sync/async versions of everything. See C# DoXxx() & DoXxxAsync() everywhere, where the only difference between the two implementations are an annotation and some keywords sprinkled throughout. If rust can do the same thing without doubling the code/api surface/documentation across all libraries that would allay the fears of many opponents.
Erlang occupies a similar space.
Go is another option that's less like Haskell/Erlang but its type system is lacking/ inconsistent and it requires garbage collection.
So Rust has a real opportunity here that's not currently occupied by other languages.
Erlang occupies a similar space.
In what sense? I feel Erlang is at the other end of the spectrum from Rust. Can you explain further?
It is possible, there are many stack swapping libraries in C that don't use garbage collection/excessive heap allocations as proof (e.g. libpth). The RFC is trying to figure that out with rust, but their approach currently requires manually annotating functions with "async," creating an incompatible calling convention with existing Rust code.
I don't know of many, I'm thinking Julia JoCaml or Red maybe a little.
but their approach currently requires manually annotating functions with "async," creating an incompatible calling convention with existing Rust code.
Can this be avoided? How else?
I think Haskell happily spawns a new OS thread when one looks like it might be going to spend a while in C code?
This looks like a huge win for Rust. The syntax seems to follow the C# and ES async/await syntaxes. With stuff like this, I can see Rust-based webdev becoming popular.
On a serious note, one of my middling priorities (unfortunately :( It always feels like you can't choose most your high priorities) is to build a rust-based website and a couple of other rust-based projects t get a feel for the language. It really dose feel like all the parts are there already and that as things get added, it'll just get easier and better.
I could introduce it for the sake of my resume, or I could use the right tool. The former seems unprofessional and unproductive outside of jumping ship and leaving your co-workers with extra baggage.
I also have some personal programming projects I've been working on where I decided to value getting them out quickly vs learning a new language, and those are, all together, half done.
I've also been debating learning modern C++ to open potentially open some doors to a slightly different career than web programming (railway signalling, probably a pipedream at this point, but).
So, all in all, I love what rust represents, but it doesn't bring value right now. Once those personal projects wrap up my goal is to have a round of rust and C++ ones to do.
"You can build stuff" is still a good summary, but lots of stuff has happened since this was last updated.
Coroutines with "color" are problematic.
As applied to Go: https://blog.mozilla.org/services/2014/03/12/sane-concurrenc...
It's largely the same thing to the compiler in that it needs to transform a method into a stack-less state machine. Whether or not the state machine is evaluated lazily on demand or concurrently is mostly irrelevant and just depends on the specific problem in question.
def gen(*items):
for x in items:
if x > 5:
yield x
for y in gen(1,2,3,4,5,6,7,8,9):
print(y)
Should print: 6
7
8
9
Does this new RFC support that kind of usage? I guess I'm not clear enough on how Futures work to figure it out myself.Are coroutines more somehow more powerful?
You would use these coroutines with tokio or mio, which are event driven frameworks that use epoll at their core on Linux.