A tuple is a sequence of values, which can be of different types. A tuple is defined with circle brackets ().

rust
// Create a tuple with 3 elements.
let stuff = ("stuff", 42, 'a');

// Annotate the types of the tuple values.
let things: (&str, u8, char) = ("things", 69, 'z');

// Tuple elements are accessed with the dot `(.)` operator 
// and their `index` number.
println!("{}", stuff.0); // => stuff
println!("{}", things.1); // => 69

// All the elements of a tuple can be destructured into a pattern.
let (a, b, c) = stuff;

Tuple Considerations

  • The number of elements in a tuple is known as its arity.
  • Traits are only implemented on a tuple with an arity of 12 or less.
  • Tuples with an arity greater than 12 lose most of their functionality.

Tags

rust