Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tuple

Tuples are similar to lists, but you cannot change their elements after you create them.

let my_tuple = (1,2,3);

// You can access elements using indexing
assert_eq(my_tuple[0], 1);

// Tuples are immutable, so assignment fails
my_tuple[0] = 99; // ERROR

// You may also iterate over tuples
for item in my_tuple {
  // ..
}

Create a 1-element tuple by adding a trailing comma:

assert_eq((1,).len(), 1);

Copy-on-write

Tuples use copy-on-write. If an operation looks like it modifies a tuple, such as appending elements, Andy C++ keeps the original tuple and creates a new one for the updated value.

let a = (1,2,3);
let b = a;
b ++= (4,5);

// A remains the same
assert_eq(a, (1,2,3));

// B was copied and (4,5) was appended
assert_eq(b, (1,2,3,4,5));

Operators

OperatorFunction
++Concatenation
<>Coerce operands into strings and concatenate
inChecks if an element is present in the list
not inChecks if an element is not present in the list
==Equality
!=Inequality
>Greater (lexicographically)
<Less (lexicographically)
>=Greater equals (lexicographically)
<=Less equals (lexicographically)