How to run code outside of select! in Rust?

  Kiến thức lập trình
loop {
    select! {
        Ok(Some(line)) = stdin.next_line() => {
            handle_input(&mut swarm, line.to_string()).await?;
        }
        event = swarm.select_next_some() => match event {
            SwarmEvent::Behaviour(P2PBehaviourEvent::Mdns(mdns::Event::Discovered(list))) => {
                for (peer_id, _multiaddr) in list {
                    println!("Discovered a new peer: {peer_id}");
                    swarm.behaviour_mut().gossipsub.add_explicit_peer(&peer_id);
                }
            },
            SwarmEvent::Behaviour(P2PBehaviourEvent::Mdns(mdns::Event::Expired(list))) => {
                for (peer_id, _multiaddr) in list {
                    println!("Peer {peer_id} has expired");
                    swarm.behaviour_mut().gossipsub.remove_explicit_peer(&peer_id);
                }
            },
            SwarmEvent::Behaviour(P2PBehaviourEvent::Gossipsub(gossipsub::Event::Message {
                propagation_source: peer_id,
                message_id: _id,
                message,
            })) => {
                handle_message(&mut swarm, peer_id, message.data).await?
            },
            SwarmEvent::NewListenAddr { address, .. } => {
                println!("Node is listening on {address}");
            },
            _ => ()
        }
    }
    // Some custom logic code...
}

Hey! I am using rust-libp2p for my personal project and I got this code from one of their examples(chat one). Now the problem I am facing is if there are no incoming message or any event I want to run my code inside the loop but the code only gets ran if there is some event like if the node got a message from another node or any new node is discovered. How can I run the code in loop without getting dependent on the events logic?

New contributor

Kamui Katsuragi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

LEAVE A COMMENT