The first version of the code works faster.
For vectors with the number of elements equal to 100_000_000
I have 26.2037 ms vs 39.9429 ms
Is there a faster way?
Considering you just need the first item in the vector i think instead of using loop you can just directly assign the first element like ... vec_02[0] = vec_01[0];
I'm new to rust tho i am up for corrections thanks:)
In the second case you're creating the 2nd vec twice, once at the top and then once when you clone. And in the first case you're breaking out of the loop after only one element (of three) is copied.
Please make sure you're showing the actual code snippets that you're using in your test.
When cloning, do not create a vec filled with zeros first. By creating the vec filled with zeros first, you are creating the new vec twice. The Vec::clone method creates a new vec.
Also, be sure to build and run the test using --release. Without this flag you are using a debug build. Performance testing with a debug build is meaningless because no optimization is performed.