How can I transform a Vector into an array?

The question is in the title.


Because the size of the vector is dynamic, this is not possible. If you know the size of your vector though, then you can create an array with that same static size and copy the elements one by one using a while loop.


Thanks. I assume something like this is what you mean for a vector/array of length 1.

let mut test = Vec::new();

let mut result = [0; 1];
result[0] = test.get(0).unwrap();
1 Like

Thanks a lot, Iā€™m going to try it!

That is exactly what I mean. Just make sure to do appropriate error checking for out-of-bound indices so that nothing bad happens :slight_smile:

1 Like

interesting, cool, thank you :yum:

1 Like

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.