Skip to content

add examples of port 0 binding behavior #111527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions library/std/src/net/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 9 additions & 0 deletions library/std/src/net/udp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down