#how do i cast captured integer in a for loop from usize into isize

1 messages · Page 1 of 1 (latest)

vestal nacelle
#

how do i cast captured integer in a for loop from usize into isize

dusky pier
#

You cant
If you need a negative start and end, youll need to use a while loop

#

Not really
For loops are only meant for iterating arrays so they just dont support this use case

nimble sierra
#

it's not that weird, considering that few releases ago zig didn't support numeric for loops at all, while loop was the standard way to have numeric iterator

#
const rununtil = 2;
var x: isize = -rununtil;
while (x < rununtil) : (x += 1) {
  // the inner loop and other logic, x will get incremented at the end of the scope
}

to avoid variable shadowing further down the scope, you can wrap it all in a block expression:

{
  var x: isize = -rununtil;
  // loop
}