Initialize a large, fixed-size array with non-Copy types

后端 未结 3 470
醉梦人生
醉梦人生 2020-11-27 08:22

I’m trying to initialize a fixed-size array of some nullable, non-copyable type, like an Option> for some kind of Thing. I’d

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 08:44

    This is the "keep it simple" answer: just type out all the values:

    struct Thing;
    const SIZE: usize = 5;
    
    fn main() {
        let array: [Option>; SIZE] = [None, None, None, None, None];
    }
    

    You could also use a build script to generate this code for you. For an example of this, see:

    • How to create a static string at compile time

提交回复
热议问题