如何访问通过 UDP 发送的数据?

How do I access the data sent over UDP?

Looking at the documentation,我只能想办法return通过UDP发送的内容的大小:

Receives data from the socket. On success, returns the number of bytes read and the address from whence the data came.

目前有输出内容的方法吗?

fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)>

数据被读入buf的开头。因此,读取的数据可以作为 &[u8] 访问,如下所示:

match socket.recv_from(buf) {
    Ok((bytes_read, _)) => Some(&buf[0..bytes_read]),
    Err(_) => None,
}