Skip to content

Commit f9bbd23

Browse files
authored
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.
2 parents e0991b7 + e33d516 commit f9bbd23

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

library/std/src/net/tcp.rs

+9
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,15 @@ impl TcpListener {
756756
/// ];
757757
/// let listener = TcpListener::bind(&addrs[..]).unwrap();
758758
/// ```
759+
///
760+
/// Creates a TCP listener bound to a port assigned by the operating system
761+
/// at `127.0.0.1`.
762+
///
763+
/// ```no_run
764+
/// use std::net::TcpListener;
765+
///
766+
/// let socket = TcpListener::bind("127.0.0.1:0").unwrap();
767+
/// ```
759768
#[stable(feature = "rust1", since = "1.0.0")]
760769
pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<TcpListener> {
761770
super::each_addr(addr, net_imp::TcpListener::bind).map(TcpListener)

library/std/src/net/udp.rs

+9
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,15 @@ impl UdpSocket {
9090
/// ];
9191
/// let socket = UdpSocket::bind(&addrs[..]).expect("couldn't bind to address");
9292
/// ```
93+
///
94+
/// Creates a UDP socket bound to a port assigned by the operating system
95+
/// at `127.0.0.1`.
96+
///
97+
/// ```no_run
98+
/// use std::net::UdpSocket;
99+
///
100+
/// let socket = UdpSocket::bind("127.0.0.1:0").unwrap();
101+
/// ```
93102
#[stable(feature = "rust1", since = "1.0.0")]
94103
pub fn bind<A: ToSocketAddrs>(addr: A) -> io::Result<UdpSocket> {
95104
super::each_addr(addr, net_imp::UdpSocket::bind).map(UdpSocket)

0 commit comments

Comments
 (0)