Post
Share your knowledge.
How can I copy a vector<u64> to use multiple times?
I'm trying to copy a vector
- Move CLI
- Move
Answers
4Essentially, if you're looking for a straightforward way, using copy arr
should suffice as long as your elements can be copied like that.
You can try writing a function like this:
public fun copyVector(arr: vector<u64>, ctx: &mut TxContext) {
let i = 0;
let newVec = vector::empty<u64>();
while (i < vector::length(&arr)) {
let element = vector::borrow_mut(&mut arr, i);
let newElement = *element;
vector::push_back(&mut newVec, newElement);
i = i + 1;
};
Since the vector elements are u64, you might consider let new_arr: vector<u64> = copy arr;
. However, it's always best to test your smart contracts on a development network to ensure the correct behavior.
The snippet let new_arr: vector<u64> = arr;
might also work. But remember, you can use arr
multiple times as each usage will result in a copy.
Do you know the answer?
Please log in and share it.
Move is an executable bytecode language used to implement custom transactions and smart contracts.