Rust Tuples Basics


A tuple is simply a concatenation of multiple values:

  • of possibly different types
  • whose number and types is known statically

For example, (1, "Hello") is a 2 elements tuple composed of a i32 and a &str, and its type is denoted as (i32, &'static str) in a similar fashion as its value.

To access an element of a tuple, one just simply uses its index:

let tuple =  (1, "Hello");
println!("First element: {}, second element: {}", tuple.0, tuple.1);

Because the tuple is built-in, it is also possible to use pattern matching on tuples:

match (1, "Hello") {
    (i, _) if i < 0 => println!("Negative integer: {}", i),
    (_, s) => println!("{} World", s),

Special Cases

The 0 element tuple: () is also called the unit, unit type or singleton type and is used to denote the absence of meaningful values. It is the default return type of functions (when -> is not specified). See also: What type is the "type ()" in Rust?.

The 1 element tuple: (a,), with the trailing comma, denotes a 1 element tuple. The form without a comma (a) is interpreted as an expression enclosed in parentheses, and evaluates to just a.

And while we are at it, trailing commas are always accepted: (1, "Hello",).


The Rust language today does not support variadics, besides tuples. Therefore, it is not possible to simply implement a trait for all tuples and as a result the standard traits are only implemented for tuples up to a limited number of elements (today, up to 12 included). Tuples with more elements are supported, but do not implement the standard traits (though you can implement your own traits for them).

This restriction will hopefully be lifted in the future.