gpt4 book ai didi

rust - 有没有办法获取对 Vec 的可变子切片的引用?

转载 作者:行者123 更新时间:2023-11-29 08:14:54 26 4
gpt4 key购买 nike

我想预分配一个向量,然后写入它的切片,包括 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

最佳答案

你想要这个:

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

关于rust - 有没有办法获取对 Vec<T> 的可变子切片的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27679858/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com