@@ -499,6 +499,19 @@ impl UdpSocket {
499
499
/// This will retrieve the stored error in the underlying socket, clearing
500
500
/// the field in the process. This can be useful for checking errors between
501
501
/// 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
+ /// ```
502
515
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
503
516
pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
504
517
self . 0 . take_error ( )
@@ -507,15 +520,36 @@ impl UdpSocket {
507
520
/// Connects this UDP socket to a remote address, allowing the `send` and
508
521
/// `recv` syscalls to be used to send data and also applies filters to only
509
522
/// 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
+ /// ```
510
532
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
511
533
pub fn connect < A : ToSocketAddrs > ( & self , addr : A ) -> io:: Result < ( ) > {
512
534
super :: each_addr ( addr, |addr| self . 0 . connect ( addr) )
513
535
}
514
536
515
537
/// Sends data on the socket to the remote address to which it is connected.
516
538
///
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
518
540
/// 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
+ /// ```
519
553
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
520
554
pub fn send ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
521
555
self . 0 . send ( buf)
@@ -526,6 +560,20 @@ impl UdpSocket {
526
560
///
527
561
/// The `connect` method will connect this socket to a remote address. This
528
562
/// 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
+ /// ```
529
577
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
530
578
pub fn recv ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
531
579
self . 0 . recv ( buf)
@@ -535,6 +583,15 @@ impl UdpSocket {
535
583
///
536
584
/// On Unix this corresponds to calling fcntl, and on Windows this
537
585
/// 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
+ /// ```
538
595
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
539
596
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
540
597
self . 0 . set_nonblocking ( nonblocking)
0 commit comments