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.
Rust
// Rust program to get value from tuple// using indexfn main() { let gfg = ("cp", "algo", "FAANG", "Data Structure"); // complete tuple println!("complete tuple = {:?} ", gfg ); // first value println!("at 0 index = {} ", gfg.0 ); // second value println!("at 0 index = {} ", gfg.1 ); // third value println!("at 0 index = {} ", gfg.2 ); // fourth value println!("at 0 index = {} ", gfg.3 );} |
Output :
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.
Rust
fn main() { // tuple with different types of values let gfg = ("cp", 10, "FAANG", 20); println!("complete tuple = {:?} ", gfg ); println!("at 0 index = {} ", gfg.0 ); println!("at 0 index = {} ", gfg.1 ); println!("at 0 index = {} ", gfg.2 ); println!("at 0 index = {} ", gfg.3 );} |
Output :
complete tuple = ("cp", 10, "FAANG", 20)
at 0 index = cp
at 0 index = 10
at 0 index = FAANG
at 0 index = 20


