Move.

Post

Share your knowledge.

Santorini.
Apr 06, 2025
Expert Q&A

How can I copy a vector<u64> to use multiple times?

I'm trying to copy a vector into a local variable because I need two instances of the same vector for my project. I've seen some methods, but I'm not entirely sure how to implement them correctly. Can anyone guide me on the best practices or methods to achieve this in the Move programming language?

  • Move CLI
  • Move
0
4
Share
Comments
.

Answers

4
BlueEyedCrypto.
Apr 6 2025, 05:12

Essentially, if you're looking for a straightforward way, using copy arr should suffice as long as your elements can be copied like that.

0
Comments
.
Bolke .
Apr 6 2025, 16:45

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;
}; 
0
Comments
.
DuAn.
Apr 7 2025, 02:27

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.

0
Comments
.
elfDani.
Apr 7 2025, 03:25

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.

0
Comments
.

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.

148Posts231Answers
Sui.X.Peera.

Earn Your Share of 1000 Sui

Gain Reputation Points & Get Rewards for Helping the Sui Community Grow.

Reward CampaignJune
      We use cookies to ensure you get the best experience on our website.
      More info