Dump everything from a futures::channel::mpsc::Receiver<String>

let r: futures::channel::mpsc::Receiver<String> = ... ;

wasm_bindgen_futures::spawn_local(async move {
  for x in ... ??? {
    println!("msg: {:?}", x);
  }
})

I am trying to figure out how to write an async task whose sole purpose in life is to dump out all msgs from a mpsc::Receiver<String> and am not figuring it out via autocompletion. Is there a trivial way to do this (or a reason it can't be done) ?

you want to use a while loop, like so: Rust Playground.

I'm using tokio rather than futures, but it's the same principle

I must be missing something obvious:

has a poll_next but no .recv()

Oh, thought the api would be more alike... My bad
That Receiver has a next method tho (due to an impl for the StreamExt trait): Receiver in futures::channel::mpsc - Rust, which returns a future that resolves to the next item, which should be what you're looking for

1 Like

I can't believe I missed it. Thanks for your patience. Everything compiles now.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.