我想预先分配一个向量,然后能够写入它的片段,包括writing to it from a TcpStream,它将“buf:& mut [u8]”作为参数。
//create a vec with 256MB capacity
let mut myvec:Vec<u8> = Vec::with_capacity(268435456);
//grow the vec to 256MB and initialized it with 0s
myvec.grow(268435456, 0x00);
//try to get a mutable slice of the first 1kb of the vec
let body_slice:&mut [u8] = myvec[10..1034];
error: cannot borrow immutable dereference of `&`-pointer as mutable
答案 0 :(得分:3)
你想要这个:
let body_slice: &mut [u8] = &mut myvec[10..1034];