我正在尝试创建一个固定长度的数组,其中包含指向切片的可选指针,并将其初始化为None
。这是我的尝试:
const NROWS: usize = 100;
fn main() {
// Trying to get something analogous to char* table[NROWS] in C
let table: [Option<Box<&mut [u8]>>; NROWS] = [None; NROWS];
}
当我尝试编译时,编译器会抱怨Copy
特性未实现:
error[E0277]: the trait bound `std::boxed::Box<&mut [u8]>: std::marker::Copy` is not satisfied
--> src/main.rs:8:50
|
8 | let table: [Option<Box<&mut [u8]>>; NROWS] = [None; NROWS];
| ^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::boxed::Box<&mut [u8]>`
|
= note: required because of the requirements on the impl of `std::marker::Copy` for `std::option::Option<std::boxed::Box<&mut [u8]>>`
= note: the `Copy` trait is required because the repeated element will be copied
我不确定如何为这种类型实现Copy
特征。我尝试将&mut [u8]
替换为我自己的Page
类型,如下所述,但这也不起作用。
#[derive(Copy, Clone)]
struct Page<'a> {
data: &'a mut [u8],
}
我需要怎么做才能编译原始代码?