Tuples are a bit of a mix between arrays and structs in they have a fixed number of elements, but they may have mixed field types and are referenced by index positions like arrays.
use fmt;
type point = (f32, f32);
export fn main() void = {
// This is a nameless tuple of 3 fields
let person = ("Blain", "Smith", 43);
// Fields are access with dot-notation of their index.
fmt::printf("{} {} {}\n", person.0, person.1, person.2)!;
let p: point = (3.14, 8.98);
fmt::printf("{} {}\n", p.0, p.1)!;
// We can deconstruct the tuple into individual variables.
let (x, y) = p;
fmt::printf("{} {}\n", x, y)!;
};
$ hare run tuples.ha
Blain Smith 43
3.14 8.98
3.14 8.98
Back to table of contents