如何在Rust中初始化指向切片的可选指针数组?

时间:2019-04-18 23:03:13

标签: rust

我正在尝试创建一个固定长度的数组,其中包含指向切片的可选指针,并将其初始化为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],
}

我需要怎么做才能编译原始代码?

0 个答案:

没有答案