How to loop through a vector in Sui move

What is the syntax for efficiently looping through a vector in Sui move?

2 Likes

I also never remember, let’s pin it once and for all :smiley:

from discord: (by Thouny)

let mut i = 0
while (i < add.length) { 
   let item = add[i]; 
   ...
   i = i + 1 
}

or

 while (!add.is_empty()) {
   let item = add.pop_back();
   ...
}
3 Likes

what about the beta.2024 macro?

let vctr = vector[u64];
vctr.push_back(1);
vctr.push_back(2);
vctr.push_back(3);

let values: u64 = 0;
vctr.for_each!(|v| values = values + v);

is this a valid vector loop?