It's been a while I've used Rust and I haven't done any async/await in Rust.
Suppose that you are parsing a json returned by a web service. The json has an array and each of the array has a node that describes a file that I need to download asynchronously. So I don't know how many calls to download_file()
I will make ahead of time.
In Go, I have WaitGroup that gives me a way to prevent the main thread from exiting before all the calls to coroutines finish. Does Rust have something similar:
Pseudo code:
let mut wg = WaitGroup<Future<()>>::new();
for link in lazy_range_of_links {
let mut f = call_to_async_download_fn();
f.add_callback(|_| { /* some cleanup */ });
wg.add(1);
wg.wait(); // waits for all wg to finish
async fn call_to_async_download_fn(wg: &mut WaitGroup<Future<()>>) -> Future<()> {
// download file
wg.done(); // send signal that this wg has completed
}