Search code examples
rusthttpserveractix-web

What is the difference between HttpServer::listen() and HttpServer::bind() in actix-web?


I'm currently working on an actix-web application but I have some difficulty in understanding how HttpServer.listen() and HttpServer.bind() are different and what do these functions do.


Solution

  • HttpServer::bind takes a (bunch of) socket address(es), resolves them, opens the corresponding connections, then listens.

    HttpServer::listen just listens.

    rustdoc has a source link next to most functions, so you can actually browse the code to bind and it's pretty straightforward:

        pub fn bind<A: net::ToSocketAddrs>(mut self, addrs: A) -> io::Result<Self> {
            let sockets = self.bind2(addrs)?;
    
            for lst in sockets {
                self = self.listen(lst)?;
            }
    
            Ok(self)
        }