Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active August 25, 2022 23:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjberger/85bc58e1b2c927d02b9b4605fa5f19e2 to your computer and use it in GitHub Desktop.
Save matthewjberger/85bc58e1b2c927d02b9b4605fa5f19e2 to your computer and use it in GitHub Desktop.

Let's break it down! 🚀

First let's look at where disconnect_receiver is declared.

let (disconnect_sender, mut disconnect_receiver) = mpsc::unbounded::<(String, Receiver<String>)>();

disconnect_receiver is an UnboundedReceiver

Notice that UnboundedReceiver<String> implements the StreamExt trait:

StreamExt provides the next() method, which gives you the next item in the stream if it's available. Because it returns a Future, we have to .await it

If we look at the examples for that next() method, we can see after .awaiting it we get an Option<T> which is either Some(T) or None

With that in mind let's look at the while let expression:

while let Some((_name, _pending_messages)) = disconnect_receiver.next().await {}

This is using the while let syntax and it's pattern matching on the result from the right hand side of the expression.

It is saying, 'while the result of the right hand side is Some((_name, _pending_messages))', do something with the values'. That Some(_) expression is pattern matching whatever is on the right hand side. Since the right hand side is either Some() or None, it's equivalent to saying this ultimately:

while the disconnect_receiver has events to process, continue

and because next() advances the iterator and we don't do anything inside the curly brackets, we are just draining the disconnect_receiver of its events

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment