[英]How to convert IoSliceMut array into Vec<u8>?
let mut buffer = Vec::with_capacity(10);
stream.read_vectored(&mut buffer);
The code above turns buffer
into an IoSliceMut
vector, but I don't know how to read from this vector or how to convert it back into a Vec<u8>
.上面的代码将
buffer
转换为IoSliceMut
向量,但我不知道如何从该向量中读取或如何将其转换回Vec<u8>
。
You don't have to convert the IoSliceMut
structs back to u8
arrays, the read_vectored
writes the bytes directly into the array buffers since you pass them by mutable references.您不必将
IoSliceMut
结构转换回u8
arrays, read_vectored
将字节直接写入数组缓冲区,因为您通过可变引用传递它们。 You can just use the buffers afterward and they will have the data written in them.之后您可以只使用缓冲区,它们会将数据写入其中。 Example:
例子:
use std::io::IoSliceMut;
use std::io::Read;
fn main() {
let mut buffer: [u8; 10] = [0; 10];
let mut io_slices = [IoSliceMut::new(&mut buffer)];
let mut data: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
data.read_vectored(&mut io_slices);
dbg!(data, buffer); // data is now in buffer
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.