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 .await
ing 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