Arrays and tuples in Leo are zero indexed.
Leo supports arrays of all types:
arrays of integers, arrays of field elements, arrays of circuits, arrays of chars, etc.
An array type is defined by both the type and the number of its elements:
for example, the type of arrays of
u8 integers of length 3
is different from
both the type of arrays of
u8 integers of length 10
and the type of arrays of
u16 integers of length 3.
Create an array with brackets
Create an array of the same element with a fixed size.
Access an element of an array starting at index 0. Indices must be a
Access a subset of an array.
from index must be a
u32: it is the first element of the range. Omitting the
from index will default to index 0.
to index must be a
u32: it is one past the last element of the range. Omitting the
to index defaults to the length of the array.
Copies the elements of one array into another. Element types must match.
An array spread may only occur inside an inline array construction expression
[..., ..., ...]:
the spread is equivalent to listing its elements in the inline array construction expression.
Leo supports static arrays with fixed size. Dynamic arrays do not exist in Leo. You cannot change the length of an array once it is initialized.
Leo features string literals for defining arrays of chars. Strings are not a type but a simple way of defining arrays of characters.
It is also possible to use string literals for comparing values (just like arrays):
Leo supports multi-dimensional arrays. These are simply arrays whose elements are arrays.
[[0; 3]; 2] is read as: An array of length 2 of arrays of length 3 with elements
For increased readability, the dimensions of a multi-dimensional array can be specified in a tuple.
[0; (2, 3)] is read the same as the bracket notation: An array of length 2 of arrays of length 3 with elements
Array tuple syntax is merely syntactic sugar for the more verbose bracket notation
Valid multidimensional arrays
- Allocate an array with 3 columns and 2 rows of type
[[u32; 3]; 2].
- We would like to return value
- Access the second row located at index 1
- Access the third column located at index 2
- Verify the program output is
Leo supports tuples of other data types.
Tuple values are accessed with a dot
. the index must be a non-negative numeric literal.