In this tutorial, Learn about Tuple data type in Rust. Tuple is one of the compound data types that stores the multiples of different data types under a single name.

Tuple Types:

Tuples are created with multiple data types and their values.

Here is the syntax.

let tuple_variable_name:(datatype1,datatype2... datatypen)=(value1,value2... valuen);
let tuple_variable_name=(value1,value2... valuen);

Tuples can be created in two ways.

  • Provide data type and data
  • Without providing data type, the compiler infers the type from the assigned data

Tuples are immutable, once declared, and will never be changed types.

Here is an example in which created an employee tuple type of integer, string, and boolean.

fn main() {
    let employee: (u32, &str, bool) = (1, "John", false);
    let employee1 = (1, "John", false);
}

How to Pretty Print and access the tuple values

Normal println does not work to print tuple values

The below example throws compilation error `cannot be formatted with the default formatting.

fn main() {
    let employee: (u32, &str, bool) = (1, "John", false);
    let employee1 = (1, "John", false);

    println!("{}", employee);
}

We can access tuple using special syntax {:?} (or {:#?}

fn main() {
    let employee: (u32, &str, bool) = (1, "John", false);
    let employee1 = (1, "John", false);

    println!("{:?}", employee); // normal print
    println!("{:#?}", employee1); // pretty print
}

Output:

(1, "John", false)
(
    1,
    "John",
    false,
)

Similarly, you can access individual elements using an index that starts with 0.

fn main() {
    let employee: (u32, &str, bool) = (1, "John", false);

    println!("{}", employee.0);
    println!("{}", employee.1);
    println!("{}", employee.2);
}

Output:

1
John
false

functions tuple arguments and return tuple

Functions take tuples as an argument and return tuples.

In the below example, Function is declared to pretty print tuple object.

  • Function provided with tuple arguments
  • Function returns multiple values of the tuple.

This is an example function tuple arguments and returning multiple values

fn main() {
    let employee: (u32, &str, bool) = (1, "John", false);

    let emp = pretty_print(employee);
    println!("{:?}", emp);
}
fn pretty_print(emp: (u32, &str, bool)) -> (u32, &str, bool) {
    println!("{:#?}", emp);
    return emp;
}

Output:

(
    1,
    "John",
    false,
)
(1, "John", false)

Tuples destructing

Destructing assignment is one of the features used to separate values from the collection of values.

Rust provides support for this in tuples.

Here is a tuple destructing assignment example.

fn main() {
    let employee: (u32, &str, bool) = (1, "John", false);

    let (id, name, active) = employee;
    println!("{}", id);
    println!("{}", name);
    println!("{}", active);
}

Difference between Tuples and Array in Rust?

Both are compound types in Rust and the following is a comparison between them.

Tuple Array
Stores values of multiple datatypes Stores the values of the same data type
Fixed Number of fields Fixed number of elements
Access the index of the individual element syntax - Tuple.index Access the elements using [] with index syntax- array[index]
Destructing assignment is possible Destructing assignment is possible
Allow you to pass as an argument in functions and return type supports functional arguments and return