Range Over Built in Nim
On this page
Range over Built-in Types in Nim
In this example, we iterate over elements in various built-in data structures using for
loops.
Here we use for
to sum the numbers in an array. Arrays and sequences work similarly in Nim.
for
loops in Nim provide both the index and value for each entry. If the index isn’t needed, we can ignore it.
for
loops in maps (dictionaries) iterate over key/value pairs.
for
loops can also iterate over just the keys of a map.
for
loops in strings iterate over Unicode code points. The first value is the byte index of the rune
, and the second the rune
itself. See the section on strings and runes for more details.
Running the translated code will produce the following output, showing the results of each operation:
Now, let’s proceed to the next example about pointers.