@@ -295,6 +295,19 @@ impl UdpSocket {
295
295
/// This will retrieve the stored error in the underlying socket, clearing
296
296
/// the field in the process. This can be useful for checking errors between
297
297
/// calls.
298
+ ///
299
+ /// # Examples
300
+ ///
301
+ /// ```no_run
302
+ /// use std::net::UdpSocket;
303
+ ///
304
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
305
+ /// match socket.take_error() {
306
+ /// Ok(Some(error)) => println!("UdpSocket error: {:?}", error),
307
+ /// Ok(None) => println!("No error"),
308
+ /// Err(error) => println!("UdpSocket.take_error failed: {:?}", error),
309
+ /// }
310
+ /// ```
298
311
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
299
312
pub fn take_error ( & self ) -> io:: Result < Option < io:: Error > > {
300
313
self . 0 . take_error ( )
@@ -303,15 +316,36 @@ impl UdpSocket {
303
316
/// Connects this UDP socket to a remote address, allowing the `send` and
304
317
/// `recv` syscalls to be used to send data and also applies filters to only
305
318
/// receive data from the specified address.
319
+ ///
320
+ /// # Examples
321
+ ///
322
+ /// ```no_run
323
+ /// use std::net::UdpSocket;
324
+ ///
325
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
326
+ /// socket.connect("127.0.0.1:8080").expect("connect function failed");
327
+ /// ```
306
328
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
307
329
pub fn connect < A : ToSocketAddrs > ( & self , addr : A ) -> io:: Result < ( ) > {
308
330
super :: each_addr ( addr, |addr| self . 0 . connect ( addr) )
309
331
}
310
332
311
333
/// Sends data on the socket to the remote address to which it is connected.
312
334
///
313
- /// The `connect` method will connect this socket to a remote address. This
335
+ /// The [ `connect()`] method will connect this socket to a remote address. This
314
336
/// method will fail if the socket is not connected.
337
+ ///
338
+ /// [`connect()`]: #method.connect
339
+ ///
340
+ /// # Examples
341
+ ///
342
+ /// ```no_run
343
+ /// use std::net::UdpSocket;
344
+ ///
345
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
346
+ /// socket.connect("127.0.0.1:8080").expect("connect function failed");
347
+ /// socket.send(&[0, 1, 2]).expect("couldn't send message");
348
+ /// ```
315
349
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
316
350
pub fn send ( & self , buf : & [ u8 ] ) -> io:: Result < usize > {
317
351
self . 0 . send ( buf)
@@ -322,6 +356,20 @@ impl UdpSocket {
322
356
///
323
357
/// The `connect` method will connect this socket to a remote address. This
324
358
/// method will fail if the socket is not connected.
359
+ ///
360
+ /// # Examples
361
+ ///
362
+ /// ```no_run
363
+ /// use std::net::UdpSocket;
364
+ ///
365
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
366
+ /// socket.connect("127.0.0.1:8080").expect("connect function failed");
367
+ /// let mut buf = [0; 10];
368
+ /// match socket.recv(&mut buf) {
369
+ /// Ok(received) => println!("received {} bytes", received),
370
+ /// Err(e) => println!("recv function failed: {:?}", e),
371
+ /// }
372
+ /// ```
325
373
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
326
374
pub fn recv ( & self , buf : & mut [ u8 ] ) -> io:: Result < usize > {
327
375
self . 0 . recv ( buf)
@@ -331,6 +379,15 @@ impl UdpSocket {
331
379
///
332
380
/// On Unix this corresponds to calling fcntl, and on Windows this
333
381
/// corresponds to calling ioctlsocket.
382
+ ///
383
+ /// # Examples
384
+ ///
385
+ /// ```no_run
386
+ /// use std::net::UdpSocket;
387
+ ///
388
+ /// let socket = UdpSocket::bind("127.0.0.1:34254").expect("couldn't bind to address");
389
+ /// socket.set_nonblocking(true).expect("set_nonblocking call failed");
390
+ /// ```
334
391
#[ stable( feature = "net2_mutators" , since = "1.9.0" ) ]
335
392
pub fn set_nonblocking ( & self , nonblocking : bool ) -> io:: Result < ( ) > {
336
393
self . 0 . set_nonblocking ( nonblocking)
0 commit comments