Skip to content

Commit 3bd2c80

Browse files
authored
Rollup merge of rust-lang#38067 - GuillaumeGomez:udp-doc, r=frewsxcv,nagisa
Add more examples to UpdSocket r? @frewsxcv
2 parents 8c3c9c2 + 8b0b2b6 commit 3bd2c80

File tree

1 file changed

+58
-1
lines changed

1 file changed

+58
-1
lines changed

src/libstd/net/udp.rs

+58-1
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,19 @@ impl UdpSocket {
499499
/// This will retrieve the stored error in the underlying socket, clearing
500500
/// the field in the process. This can be useful for checking errors between
501501
/// calls.
502+
///
503+
/// # Examples
504+
///
505+
/// ```no_run
506+
/// use std::net::UdpSocket;
507+
///
508+
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
509+
/// match socket.take_error() {
510+
/// Ok(Some(error)) => println!("UdpSocket error: {:?}", error),
511+
/// Ok(None) => println!("No error"),
512+
/// Err(error) => println!("UdpSocket.take_error failed: {:?}", error),
513+
/// }
514+
/// ```
502515
#[stable(feature = "net2_mutators", since = "1.9.0")]
503516
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
504517
self.0.take_error()
@@ -507,15 +520,36 @@ impl UdpSocket {
507520
/// Connects this UDP socket to a remote address, allowing the `send` and
508521
/// `recv` syscalls to be used to send data and also applies filters to only
509522
/// receive data from the specified address.
523+
///
524+
/// # Examples
525+
///
526+
/// ```no_run
527+
/// use std::net::UdpSocket;
528+
///
529+
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
530+
/// socket.connect("127.0.0.1:8080").expect("connect function failed");
531+
/// ```
510532
#[stable(feature = "net2_mutators", since = "1.9.0")]
511533
pub fn connect<A: ToSocketAddrs>(&self, addr: A) -> io::Result<()> {
512534
super::each_addr(addr, |addr| self.0.connect(addr))
513535
}
514536

515537
/// Sends data on the socket to the remote address to which it is connected.
516538
///
517-
/// The `connect` method will connect this socket to a remote address. This
539+
/// The [`connect()`] method will connect this socket to a remote address. This
518540
/// method will fail if the socket is not connected.
541+
///
542+
/// [`connect()`]: #method.connect
543+
///
544+
/// # Examples
545+
///
546+
/// ```no_run
547+
/// use std::net::UdpSocket;
548+
///
549+
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
550+
/// socket.connect("127.0.0.1:8080").expect("connect function failed");
551+
/// socket.send(&[0, 1, 2]).expect("couldn't send message");
552+
/// ```
519553
#[stable(feature = "net2_mutators", since = "1.9.0")]
520554
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
521555
self.0.send(buf)
@@ -526,6 +560,20 @@ impl UdpSocket {
526560
///
527561
/// The `connect` method will connect this socket to a remote address. This
528562
/// method will fail if the socket is not connected.
563+
///
564+
/// # Examples
565+
///
566+
/// ```no_run
567+
/// use std::net::UdpSocket;
568+
///
569+
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
570+
/// socket.connect("127.0.0.1:8080").expect("connect function failed");
571+
/// let mut buf = [0; 10];
572+
/// match socket.recv(&mut buf) {
573+
/// Ok(received) => println!("received {} bytes", received),
574+
/// Err(e) => println!("recv function failed: {:?}", e),
575+
/// }
576+
/// ```
529577
#[stable(feature = "net2_mutators", since = "1.9.0")]
530578
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
531579
self.0.recv(buf)
@@ -535,6 +583,15 @@ impl UdpSocket {
535583
///
536584
/// On Unix this corresponds to calling fcntl, and on Windows this
537585
/// corresponds to calling ioctlsocket.
586+
///
587+
/// # Examples
588+
///
589+
/// ```no_run
590+
/// use std::net::UdpSocket;
591+
///
592+
/// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
593+
/// socket.set_nonblocking(true).expect("set_nonblocking call failed");
594+
/// ```
538595
#[stable(feature = "net2_mutators", since = "1.9.0")]
539596
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
540597
self.0.set_nonblocking(nonblocking)

0 commit comments

Comments
 (0)