【发布时间】:2015-02-25 02:34:44
【问题描述】:
我想预先分配一个向量,然后写入它的切片,包括 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 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];
error[E0308]: mismatched types
--> src/lib.rs:9:33
|
9 | let body_slice: &mut [u8] = myvec[10..1034];
| --------- ^^^^^^^^^^^^^^^
| | |
| | expected `&mut [u8]`, found slice `[u8]`
| | help: consider mutably borrowing here: `&mut myvec[10..1034]`
| expected due to this
【问题讨论】: