Canonical loop structure for reductions (opens in new tab)
One pattern that might be better sometimes is to name the intermediate value differently: var acc: u32 = 0; for (items) |item| acc = @max(acc, item); const max = acc; This leaves a “garbage” variable in scope, but you get the final result as const and not var without extra indentation. If it gets a bit bigger though, I think I will often go for the labeled block instead to not pollute the scope needlessly. Edit: I actually skimmed over the part of your post where you mention this so my commen...
Read the original article