Tuple

Tuple in Rust is a custom compound data type which 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-with-datatypes

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.

For this, we need a dot operator (.) and prefix it with the index value after the tuple_name while printing it.

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);
   
}

In the above program, it is shown that the tuple index 2 and index 4 is printed.

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 data 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 the data. But the tuple interprets the data the same way, it interpreted for the declaration along with the data type.

To prove that, the tuple is initialized the same way internally, it was initialized before, we execute the above program and see whether the values are stored in the index 0, index 1 and index 2.

Program Output :

rust-tuple-direct-init

In the above program output, we can see that the values in the indexes of the tuple are printed.

Destructing (Copying a tuple into another)

A tuple (the values of a tuple) can be copied to a variable and then the values can be copied back to another empty tuple from that variable. This is very interesting to understand, how this can be done.

In the below program we shall declare and initialize a tuple. And then we shall assign the tuple to a variable and we shall pass the variable into an user-defined-function. Inside the user-defined function, we shall assign the variable to an empty tuple. The tuple will now contain the same values as the earlier one.

A program to copy a tuple using function :

fn main() {
 let b: (char, bool) = ('A', true);
 copy_tuple(b);
}
fn copy_tuple(x: (char, bool)) {
  println!("Inside print method");
  let (grade, number) = x;
  println!("Grade {} , is a {} record
   ",grade,number);
  }

In the above program, b is a variable which is assigned the tuple (char, bool) = ('A', true). The variable b is then passed to the function copy_tuple(),

Program Output :

rust-tuple-copy

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions