Tuples
If you want to combine a certain number of values of different types, then you can collect them in a tuple, enclosed between parentheses (( )) and separated by commas, like this:
// from Chapter 4/code/tuples.rs
let thor = ("Thor", true, 3500u32);
println!("{:?}", thor); // ("Thor", true, 3500) The type of thor is (&str, bool, u32), that is, the tuple of the item's types.
To extract an item on index use a dot syntax:
println!("{} - {} - {}", thor.0, thor.1, thor.2); Another way to extract items to other variables is by destructuring the tuple:
let (name, _, power) = thor;
println!("{} has {} power points ", name, power); This prints out the following output:
Thor has 3500 power pointsHere the let statement matches the pattern on the left with the right-hand side. The _ indicates that we are not interested in the second item of thor.
Tuples can only be assigned to one another or compared with each other if they are of the same type. A one-element tuple needs to be written like this...