How do I initialize an array of vectors?

前端 未结 3 1901
我在风中等你
我在风中等你 2021-01-01 10:05

I would like to create an array of vectors:

fn main() {
    let v: [Vec; 10] = [Vec::new(); 10];
}

However, the compiler gives me t

3条回答
  •  天涯浪人
    2021-01-01 10:15

    You can only instantiate arrays in such fashion if the type implements the Copy trait. This trait is only for types that can be copied byte by byte (and since vector points to heap, it can't be implemented).

    One answer to this problem is the array_init crate that provides much more general way of initializing arrays in complicated fashion.

    let multi: [Vec; 10] = array_init::array_init(|_| vec![0; 24]);
    

提交回复
热议问题