Tuple in Rust

Tuple in Rust is a custom compound data type that can store one or more types of data at a time. A tuple with a single name can be initialized with different values of different data types.

The advantage of using tuple is that different values can be assigned to the tuple which are of different data types, and also different values can be accessed by the single tuple name.

The indexing of a tuple starts from 0. And the size of a tuple cannot grow or shrink at the run time.

There are more than one ways of declaring and initializing tuple in Rust:

  • Declaring and initializing a tuple along with the datatypes.
  • Declaring and initializing a tuple directly, not mentioning the datatypes.
Declaring and initializing a tuple along with the datatypes:

The syntax for tuple (with datatype)

let tuple_name: (datatype1, datatype2, datatype3) = (value1, value2, value3);

Example Program to display values of different data-types :

fn main() {
   let my_tuple:(i32,f64,u8,char,&str, bool) = (-35,100.2, 35,'a', "hello", false);
   println!("{:?}",my_tuple);
}

As shown in the above program, we have declared a tuple called my_tuple. And inside the brackets ( and ), we have mentioned the datatypes- i32, f64, u8, char, &str, and bool. And initialized the tuple with the corresponding values after the assignment operator(=).

Now, when the tuple is printed directly, with the macro println!(), it prints all the values inside the tuple. A tuple by default will print all its values starting from the index 0.

Program Output :

rust-tuple-simple

It may be noted that, if no index is mentioned, then all the values of the tuples a printed serially. However, we may mention the index and get the desired value from the tuple and also the order of retrieval of values can be altered.

Suppose, we need only the unsigned integer and the string to be printed. As the indexing starts from 0, thus, the unsigned value- 35 is at index 2 and the string- hello is at index 4.

To access a value corresponding to a particular index, the index can be used with a dot(.) operator after the tuple name.

Syntax :

println!("{:?}",tuple_name.index_value);   //index value like 0, 1, ...

Program to print values at index 2 and index 4.

fn main() {
   let my_tuple:(i32,f64,u8,char,&str, bool) = (-35,100.2, 35,'a', "hello", false);
   println!("The data in index 2 is {:?} ",my_tuple.2);
   println!("The data in tuple index 4 is{:?}",my_tuple.4);
   
}

Program Output :

rust-tuple-with-data-type

Declaring and initializing a tuple directly (without mentioning datatype)

A tuple in Rust can be directly declared as well as initialized without, mentioning the datatype during the time of its initialization. A tuple directly recognizes the data type when the values are assigned.

Syntax :

 let tuple_name = (value1_of_type1, value2_of_type2,.....)

Example Program :

fn main() {
 let tuple = (-3, 4.9, 'a');
 println!("The unsigned integer is :{:?}", tuple .0);
 println!("The floating point number is :{:?}", tuple .1);
 println!("The character is  :{:?}", tuple .2);
}

As shown in the above program, we have not mentioned any data type while declaring and initializing the tuple. Simply, the tuple is assigned with values. Here, the tuple interprets the data types of the values internally.

Program Output :

rust-tuple-direct-init

A program see the data-types of values stored in a tuple when the data types are not declared!

Add here... !

Destructuring (Copying a tuple into multiple variables)

Destructuring is a way of assigning multiple variables with a tuple, for example, if a tuple has three values, we can assign these three values into three different variables.

A program to copy a tuple using function :

fn main() {
    let original_tuple: (char, i32) = ('A', 30);
    println!("before destructuring");
    println!("{:?}", original_tuple);
    let (grade, pass) = original_tuple;
    println!("after destructuring");
    println!("Got a Grade {} , and it is a pass mark: {} ", grade, pass);
}

In the above program, original_tuple is a variable which is assigned the tuple (char, i32) = ('A', 30). The variable original_tuple is then destructured into two different variables called grade and pass.

Program Output :

rust-tuple-destructuring

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Subscribe to See Videos

Subscribe to my Youtube channel for new videos : Subscribe Now