Problem with passing data to another Future (Stream) while previous is still running


The problem is a bit confusing but i will try to explain as much as i can :slight_smile:

So imagine we have some logic like that:

        .map_err(|e| println!("Error is: {}", e))
        .fold(streamWrite, move |writer, anotherStream| {
             anotherStream.fold(streamWrite, move |writer, data| {
                  // Do some logic with `data`

What i am trying to do is somewhere in between of the streamRead data stop sending it to the streamRead fold and instead start executing anotherStream fold and pass the data which is coming to streamRead into anotherStream fold method. Is there any way to do so.

Thank you very much :slight_smile:



From code it looks like you have a stream that have another streams like items? In that case maybe Stream method flatten can help.

But from description it looks like you try to use first stream until certain point, and then starting to use another different stream - in that case something like this might help:

fn main() {
    let stream1 = futures::stream::iter_ok::<_, ()>(1..10);
    let stream2 = futures::stream::iter_ok::<_, ()>(101..110);
     let future = stream1
        .take_while(|x| Ok(*x<5))
        .fold((), |_,x| {println!("First part {}", x); Ok(())})
        .and_then(|_| {
            stream2.fold((), |_,x| {println!("Second part {}", x); Ok(())})
    future.wait().expect("Future failed");


Thanks for your response :). Main problem is that data comes from the socket and it comes into stream1 but i need some howto sent it into stream2.fold() method instead.



Not exactly sure what you need - you need all data from stream1 in stream2.fold? Then I guess you should use collect on first stream. Or you need individual items from both stream - then maybe zip method?

1 Like