有没有办法获得对Vec <t>的可变子切片的引用?</t>

时间:2014-12-28 19:26:35

标签: rust slice

我想预先分配一个向量,然后能够写入它的片段,包括writing to it from a TcpStream,它将“buf:&amp; 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 

1 个答案:

答案 0 :(得分:3)

你想要这个:

let body_slice: &mut [u8] = &mut myvec[10..1034];