Is there a more idiomatic way to initialize an array with random numbers than a for loop?

后端 未结 1 828
青春惊慌失措
青春惊慌失措 2020-12-06 18:32

Is there an idiomatic way of initialising arrays in Rust. I\'m creating an array of random numbers and was wondering if there is a more idiomatic way then just doing a for l

相关标签:
1条回答
  • 2020-12-06 19:19

    Various sized arrays can be directly randomly generated:

    use rand; // 0.7.3
    
    fn main() {
        let my_array: [u64; 8] = rand::random();
        println!("{:?}", my_array);
    }
    

    Currently, this only works for arrays of size from 0 to 32 (inclusive). Beyond that, you will want to see related questions:

    • How can I initialize an array using a function?
    • What is the proper way to initialize a fixed length array?
    0 讨论(0)
提交回复
热议问题