I already worked with coroutines in various other languages, mostly Kotlin & Go.
Now I'm practising rust and wanted to use rust's futures-library (0.3.5) for similar purposes.
Probably I'm using it wrong, but I'm not able to use rust's async-await mechanism the way I'm familar with.
An example case:
I have a Vec of futures, each doing a network request. Now I want to perform all of them in parallel. But the only wa to "execute" these function is to .await them one by one.
I actually don't see any difference to performing all requests synchronously.
Is there a way to "dispatch" the execution of async functions in parallel similar to the mechanism used in Go?
As @Cerber-Ursi mentioned you can use something like join_all or FuturesUnordered. And if you just want to "fire off" a task you can use some executor specific spawn method like tokio::spawn.
This code prints "n done" each second and is finished after vec.len() seconds.
My expactation would be that all futures print "at once" and the program execution is finished after roughly one second.
This is simulating a CPU-intensive calculation by tying up the current thread and preventing it from doing anything else. For that sort of workload, regular threading is the preferred method in Rust— async and futures are optimized for waiting on I/O operations.
Note that this AFAIK wouldn't work at least in Kotlin, too: you can't Thread.sleep() in coroutine and have other coroutines executing at that time, you have to use special delay() function (which is itself async, or, in Kotlin terms, suspendable).