In Rust, does code like stream::iter(slice).then(myasyncfun).collect_vec().await execute…
- User
In Rust, does code like
stream::iter(slice).then(my_async_fun).collect_vec().awaitexecute concurrently or not? Explain the reasoning. - Assistant
In Rust, the code
stream::iter(slice).then(my_async_fun).collect_vec().awaitdoes not execute concurrently.The
.then(my_async_fun)part appliesmy_async_funto each item in the stream, but it does not execute them concurrently. It processes each item in the stream sequentially, waiting for each future to complete before moving on to the next one.Using
then()does not introduce concurrency because it is a form of chaining, and chaining is sequential in nature. Even thoughmy_async_funis an async function, it will not be fired off concurrently with other invocations of the function for different items in the stream.If you want to execute the function concurrently on all items in the stream, you can use the or methods, which allow to set a limit on the number of futures running concurrently.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.