Skip to content

Commit 8b0b2b6

Browse files
Add more examples to UpdSocket
1 parent 1c44857 commit 8b0b2b6

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
@@ -295,6 +295,19 @@ impl UdpSocket {
295295
/// This will retrieve the stored error in the underlying socket, clearing
296296
/// the field in the process. This can be useful for checking errors between
297297
/// 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+
/// ```
298311
#[stable(feature = "net2_mutators", since = "1.9.0")]
299312
pub fn take_error(&self) -> io::Result<Option<io::Error>> {
300313
self.0.take_error()
@@ -303,15 +316,36 @@ impl UdpSocket {
303316
/// Connects this UDP socket to a remote address, allowing the `send` and
304317
/// `recv` syscalls to be used to send data and also applies filters to only
305318
/// 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+
/// ```
306328
#[stable(feature = "net2_mutators", since = "1.9.0")]
307329
pub fn connect<A: ToSocketAddrs>(&self, addr: A) -> io::Result<()> {
308330
super::each_addr(addr, |addr| self.0.connect(addr))
309331
}
310332

311333
/// Sends data on the socket to the remote address to which it is connected.
312334
///
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
314336
/// 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+
/// ```
315349
#[stable(feature = "net2_mutators", since = "1.9.0")]
316350
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
317351
self.0.send(buf)
@@ -322,6 +356,20 @@ impl UdpSocket {
322356
///
323357
/// The `connect` method will connect this socket to a remote address. This
324358
/// 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+
/// ```
325373
#[stable(feature = "net2_mutators", since = "1.9.0")]
326374
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
327375
self.0.recv(buf)
@@ -331,6 +379,15 @@ impl UdpSocket {
331379
///
332380
/// On Unix this corresponds to calling fcntl, and on Windows this
333381
/// 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+
/// ```
334391
#[stable(feature = "net2_mutators", since = "1.9.0")]
335392
pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> {
336393
self.0.set_nonblocking(nonblocking)

0 commit comments

Comments
 (0)