Rust – Tuple
A tuple in rust is a finite heterogeneous compound data type, meaning it can store more than one value at once. In tuples there is no inbuilt method to add elements into a tuple. We can use the index to get the value of a tuple, and we also can not iterate over a tuple using for loop.
Tuples in Rust are defined using small brackets as shown below :
Syntax: ("geeksforgeeks", 1, 'geek')
It is important to note that tuples are a sequence in Rust. This means its elements can be accessed by the position which is also known as tuple indexing.
Example 1: Below is the rust program to get values in a tuple.
complete tuple = ("cp", "algo", "FAANG", "Data Structure") at 0 index = cp at 0 index = algo at 0 index = FAANG at 0 index = Data Structure
Example 2: Here we will use strings and integers both in tuples.
complete tuple = ("cp", 10, "FAANG", 20) at 0 index = cp at 0 index = 10 at 0 index = FAANG at 0 index = 20