nim-basics
nim-basics copied to clipboard
Use of counter variable
If we also need to have an iteration counter (starting from zero), we can achieve that by using for <counterVariable>, <loopVariable> in
: syntax. This is very practical if you want to iterate through one iterable, and simultaneously access another iterable at the same offset.
However, as far as I know, this works only with strings, it does not work for iterators in general (for example countTo fails). However, it does work on containers. At least in my Nim 1.4.0.
So I suggest changing the text to:
If we also need to have an iteration counter (starting from zero), we can achieve that by using for <counterVariable>, <loopVariable> in
: syntax. This is very practical if you want to iterate through one container, and simultaneously access another container at the same offset. Containers are explained just a little bit later.
However, as far as I know, this works only with strings
It works for all containers (even custom ones) which have pairs
iterator. https://nim-lang.github.io/Nim/manual.html#iterators-and-the-for-statement-implicit-itemsslashpairs-invocations
Ah, so a more specific type of containers than just general containers. However, as soon as I read iterators
in the tutorial, I tried countTo
and failed, got confused and then tried string and some containers. But I saw already other Nim tutorials and program in Python.
As the text follows directly examples of iterators, some caution for new users would, I think, be helpful.