Initialize array holding struct more efficiently

前端 未结 2 1365
一个人的身影
一个人的身影 2021-01-22 16:39

I have the following code:

const N: usize = 10000;
const S: usize = 7000;

#[derive(Copy, Clone, Debug)]
struct T {
    a: f64,
    b: f64,
    f: f64
}

fn main         


        
2条回答
  •  一整个雨季
    2021-01-22 17:23

    You can use std::mem::uninitialized(). Note, however, that it is considered unsafe and needs to be marked as such:

    let mut t: [T; N] = unsafe { std::mem::uninitialized() };
    

    As stated by the aforelinked docs:

    This is useful for FFI functions and initializing arrays sometimes, but should generally be avoided.

提交回复
热议问题