Produce a Stream out of the result of a previous finished Stream

Viewed 121

In Rust and with a Stream that produces Vec<u8>, how can I consume the result and build a new stream of u8?

For example a stream that looks like:

let a: Vec<Vec<u8>> = vec![vec![1,2,3], vec![4,5,6]];
let my_stream = futures::stream::iter(a);

I would like to consume and map it to end up with:

assert_eq!(vec![1,2,3,4,5,6], my_stream.collect::<Vec<_>>().await);
1 Answers

This is normally a job for flatten, but it expects the stream to produce other streams. You can add .map(futures::stream::iter) to adapt the inner vectors to streams, or combine both into a call to flat_map:

let my_stream = my_stream.flat_map(futures::stream::iter);

Playground

Related