How to default-initialize a struct containing an array in Rust?

前端 未结 4 624
难免孤独
难免孤独 2021-01-11 10:31

What is the recommended way to declare a struct that contains an array, and then create a zero-initialized instance?

Here is the struct:

#[derive(De         


        
4条回答
  •  南笙
    南笙 (楼主)
    2021-01-11 11:25

    If you're sure to initialize every field with zero, this would work:

    impl Default for Histogram {
        fn default() -> Histogram {
            unsafe { std::mem::zeroed() }
        }
    }
    

提交回复
热议问题