我想预先分配一个向量,然后写入它的切片,包括从 a 写入它TcpStream,它以 abuf: &mut [u8]作为参数。
// Create a vec with 256MB capacity
let mut myvec: Vec<u8> = Vec::with_capacity(268435456);
// Grow the vec to 256MB and initialize it with zeroes
myvec.resize(268435456, 0x00);
// Try to get a mutable slice of the first 1kb of the vec
let body_slice: &mut [u8] = myvec[10..1034];
Run Code Online (Sandbox Code Playgroud)
// Create a vec with 256MB capacity
let mut myvec: Vec<u8> = Vec::with_capacity(268435456);
// Grow the vec to 256MB and initialize it with zeroes
myvec.resize(268435456, 0x00);
// Try to get a mutable slice of the first 1kb of the vec
let body_slice: &mut [u8] = myvec[10..1034];
Run Code Online (Sandbox Code Playgroud)
你要这个:
let body_slice: &mut [u8] = &mut myvec[10..1034];
Run Code Online (Sandbox Code Playgroud)