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

后端 未结 3 450
醉梦人生
醉梦人生 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:31

    You could use the Default trait to initialize the array with default values:

    let array: [Option<Box<Thing>>; SIZE] = Default::default();
    

    See this playground for a working example.

    Note that this will only work for arrays with up to 32 elements, because Default::default is only implemented for up to [T; 32]. See https://doc.rust-lang.org/std/default/trait.Default.html#impl-Default-98

    0 讨论(0)
  • 2020-11-27 08:32

    I'm copying the answer by chris-morgan and adapting it to match the question better, to follow the recommendation by dbaupp downthread, and to match recent syntax changes:

    use std::mem;
    use std::ptr;
    
    #[derive(Debug)]
    struct Thing {
        number: usize,
    }
    
    macro_rules! make_array {
        ($n:expr, $constructor:expr) => {{
            let mut items: [_; $n] = mem::uninitialized();
            for (i, place) in items.iter_mut().enumerate() {
                ptr::write(place, $constructor(i));
            }
            items
        }}
    }
    
    const SIZE: usize = 50;
    
    fn main() {
        let items = unsafe { make_array!(SIZE, |i| Box::new(Some(Thing { number: i }))) };
        println!("{:?}", &items[..]);
    }
    

    Note the need to use unsafe here: The problem is that if the constructor function panic!s, this would lead to undefined behavior.

    0 讨论(0)
  • 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<Box<Thing>>; 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
    0 讨论(0)
提交回复
热议问题