In Nim, an array is a fixed-size sequence of elements of a specific type. Arrays are useful in some scenarios, but in typical Nim code, sequences are more common due to their dynamic nature.
Note that arrays in Nim are displayed in the form [v1, v2, v3, ...] when printed with echo.
To run the program, save it as arrays.nim and use the Nim compiler:
In this Nim version:
We use array[5, int] to declare a fixed-size array of 5 integers.
Nim uses zero-based indexing, similar to many other programming languages.
The len() procedure is used to get the length of an array.
Nim allows array initialization using array literals [1, 2, 3, 4, 5].
Nim supports array comprehensions, which can be used for more complex initializations.
Multi-dimensional arrays are created by nesting array types.
The fmt string interpolation is used for formatting output strings.
Nim’s arrays are similar to those in many other languages, providing a fixed-size container for elements of the same type. However, for more flexible collections, Nim also offers sequences, which can grow or shrink as needed.