diff options
| author | Dylan DPC <99973273+Dylan-DPC@users.noreply.github.com> | 2023-05-18 10:52:34 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-18 10:52:34 +0530 |
| commit | f9bbd23adf7fff8676c6c691ec45c0204407af25 (patch) | |
| tree | c216a6fde5cc1fd0a897ac124c0a7326430cee2c | |
| parent | e0991b7bcd0e90a9867b013c15123350cecd095c (diff) | |
| parent | e33d5169e81ee8991b03f58550254a17c547fffc (diff) | |
| download | rust-f9bbd23adf7fff8676c6c691ec45c0204407af25.tar.gz rust-f9bbd23adf7fff8676c6c691ec45c0204407af25.zip | |
Rollup merge of #111527 - knickish:bind_port_0_documentation, r=Mark-Simulacrum
add examples of port 0 binding behavior Was trying to find the method to specify the IP address but not the port, and there wasn't information easily accessible about it in the `TcpListener` or `SocketAddr`. Adding examples to `TcpListener` and `UdpSocket` for clarity.
| -rw-r--r-- | library/std/src/net/tcp.rs | 9 | ||||
| -rw-r--r-- | library/std/src/net/udp.rs | 9 |
2 files changed, 18 insertions, 0 deletions
diff --git a/library/std/src/net/tcp.rs b/library/std/src/net/tcp.rs index 541e95d229b..141a18a42dd 100644 --- a/library/std/src/net/tcp.rs +++ b/library/std/src/net/tcp.rs @@ -756,6 +756,15 @@ impl TcpListener { /// ]; /// let listener = TcpListener::bind(&addrs[..]).unwrap(); /// ``` + /// + /// Creates a TCP listener bound to a port assigned by the operating system + /// at `127.0.0.1`. + /// + /// ```no_run + /// use std::net::TcpListener; + /// + /// let socket = TcpListener::bind("127.0.0.1:0").unwrap(); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> { super::each_addr(addr, net_imp::TcpListener::bind).map(TcpListener) diff --git a/library/std/src/net/udp.rs b/library/std/src/net/udp.rs index 9628bcc5108..5ca4ed832f3 100644 --- a/library/std/src/net/udp.rs +++ b/library/std/src/net/udp.rs @@ -90,6 +90,15 @@ impl UdpSocket { /// ]; /// let socket = UdpSocket::bind(&addrs[..]).expect("couldn't bind to address"); /// ``` + /// + /// Creates a UDP socket bound to a port assigned by the operating system + /// at `127.0.0.1`. + /// + /// ```no_run + /// use std::net::UdpSocket; + /// + /// let socket = UdpSocket::bind("127.0.0.1:0").unwrap(); + /// ``` #[stable(feature = "rust1", since = "1.0.0")] pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> { super::each_addr(addr, net_imp::UdpSocket::bind).map(UdpSocket) |
