INADDR_ANY for Rust UDP sockets

INADDR_ANY doesn't seem to be defined anywhere in Rust land. In C, to bind a socket in a client in preparation for sending, you'd bind to INADDR_ANY, let it bind to some or all available interfaces, and go from there. INADDR_ANY is just the zero IP address.

So I tried

let mut socket = UdpSocket("0")?;

That yields an "Invalid socket address" error. What's the "Any" address in Rust?

let mut socket = UdpSocket::bind("")?;

Fair enough. There really should be a named constant for that, though.


Yeah, I agree. At least an example in the docs until then.

1 Like

Right. The docs all show talking to, which is "localhost" and only useful for talking to yourself.

It does have a name: std::net::Ipv4Addr - Rust


Ah, that's where it is. Thanks. That should be used in the UDP documentation examples.

I've written a PR to make this easier to find in the future: Add documentation to help people find `IpAddr::UNSPECIFIED` by joshtriplett · Pull Request #84257 · rust-lang/rust · GitHub


The feedback loop between the Rust users and developers is quite amazing.


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.