Does Rust have an equivalent to Go's WaitGroup?

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
}
2 Likes

Ok I think std::sync::Barrier is similar to WaitGroup and it works on threads.

There is also crossbeam's WaitGroup, which dynamically figures out how many waiters are needed. The downside being that it is a one-shot wait group.

There is also the simpler join_all and try_join_all. join_all's docs also mentions FuturesUnordered. I have never used it but it looks like it is better when you have a lot of futures.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.