I want to create an array of 10 empty vectors in Rust, but [Vec::new(); 10]
doesn't work as Vec
doesn't implement Copy
. How can I do this, and in more general terms how can I initialize a array by repeatedly calling a function?
相关问题
- How to get the maximum of more than 2 numbers in V
- Faster loop: foreach vs some (performance of jsper
- Convert Array to custom object list c#
- pick a random item from a javascript array
- Newtonsoft DeserializeXNode expands internal array
相关文章
- How can I convert a f64 to f32 and get the closest
- Numpy matrix of coordinates
- What is a good way of cleaning up after a unit tes
- PHP: Can an array have an array as a key in a key-
- How do I get characters common to two vectors in C
- Accessing an array element when returning from a f
- How can I convert a PHP function's parameter l
- How to make a custom list deserializer in Gson?
For your specific case, you can just use
Default
:For the general case, you can create an iterator out of your function and then collect into the array using
ArrayVec
:I see two possible approaches
First
A simple solution using macro
It's a bit verbose, but even the standard library uses this kind of construct.
Second
After realizing a connection between this question and another that I had answered before, I wrote this solution using nodrop
And a test that indicates that it does not leak memory
In this test, the method
X::new
panics when creatingX(3)
, soX(0)
,X(1)
,X(2)
must be dropped.Others
There is an unsafe solution here.