Hare by Example: Arrays

Arrays are ordered values of the same type of members determined at compile time. Like most languages they are fixed in length and cannot grow, shrink, or contain mixed types.

use fmt;

export fn main() void = {
	// Arrays must be initialized with default values
	let arr: [5]int = [1, 2, 3, 4, 5];

	// We can even fill them up with values with the ... operator
	// which will fill the remaining elements with 3
	let lots: [1024]int = [1, 2, 3...];

	// Most of the time you'd just fill them with zeros or some
	// zero-like value
	let zeros: [1024]int = [0...];

	// You may also specify the size with the values only by
	// omitting the size
	let infer: [_]int = [1, 2, 3, 4, 5];

	// Indexes are 0-based like other languages
	infer[3] = 444;

	for (let i = 0z; i < len(infer); i += 1) {
		fmt::println(infer[i])!;
	};

	// len(will) return the size of the array as a `size` type. The
	// length here is 5z;
	fmt::println(len(infer))!;
};
$ hare run arrays.ha
4/4 tasks completed (100%)
1
2
3
444
5
5