Question: 1) [API for send here](https://doc.rust-lang.org/nightly/std/net/struct.UdpSocket.html#method.send_to) returns `Result<usize>`. Why is that ? In my head, a UDP send is all or none. The return value seems to suggest that sending can succeed but entire data may not be written which makes me code like:

```
let mut bytes_written = 0;
while bytes_written < data.len() {
    bytes_written += match udp_socket.send_to(&data[bytes_written..]) {
         Ok(bytes_tx) => bytes_tx,
         Err(_) => break,
    }
}

```

Recently someone told me this is completely unnecessary. But I don't understand. If that were true why is the return not `Result<()>` instead, which is also what i was expecting ?

2)
[For reads](https://doc.rust-lang.org/nightly/std/net/struct.UdpSocket.html#method.recv_from) though I understand. I could give it a buffer of size 100 bytes but the datagram might only be 50 bytes long. So essentially I should utilise only `read_buf[..size_read]`. Here my question is what happens if the buffer size is 100 but the datagram size is say 150 bytes ? Will `recv_from` fill in only 100 bytes and return `Ok(100, some_peer_addr)` ? If i re-read will it fill in the remaining of the datagram ? What if another datagram of 50 bytes arrived before my second read ? Will i get just the remaining 50 bytes the second time and 50 bytes of new datagram the 3rd time or complete 100 bytes the 2nd time which also contains the new datagram ? Or will be an error and i will lose the 1st datagram on my initial read and never be able to recover it ?

Answer: 
The answer to both of these questions lies in the documentation of the respective BSD sockets functions, `sendto()` and `recvfrom()`. If you use some \*nix system (OS X or Linux, for example), you can use `man sendto` and `man recvfrom` to find it.

1) `sendto()` man page is rather vague on this; [Windows](https://msdn.microsoft.com/ru-ru/library/windows/desktop/ms740148(v=vs.85).aspx) API page explicitly says that it is possible for the return value be less than `len` argument. See also [this](https://stackoverflow.com/questions/8900474/when-will-send-return-less-than-the-length-argument) question. It looks like that this particular moment is somewhat under-documented. I think that it is probably safe to assume that the return value will always be equal either to `len` or to the error code. Problems may happen if the length of the data sent through `sendto()` exceeds the internal buffer size inside the OS kernel, but it seems that at least Windows will return an error in this case.

2) `recvfrom()` man page unambiguously states that the part of a datagram which does not fit into the buffer will be discarded:

> 
> The recvfrom() function shall return the length of the message
>  written to the buffer pointed to by the buffer argument. For
>  message-based sockets, such as SOCK\_RAW, SOCK\_DGRAM, and
>  SOCK\_SEQPACKET, the entire message shall be read in a single
>  operation. If a message is too long to fit in the supplied buffer,
>  and MSG\_PEEK is not set in the flags argument, the excess bytes shall
>  be discarded.
> 
> 
> 

So yes, `recv_from()` will fill exactly 100 bytes, the rest will be discarded, and further calls to `recv_from()` will return new datagrams.
If you [dig down](https://github.com/rust-lang/rust/blob/464cdff102993ff1900eebbf65209e0a3c0be0d5/src/libstd/sys/common/net.rs#L374), it just wrapping the [C sendto function](http://pubs.opengroup.org/onlinepubs/009695399/functions/sendto.html). This function returns number of bytes sent, so Rust just passes that on (while handling the -1 case and turning errno into actual errors).