|
| 1 | +# Introduction |
| 2 | + |
| 3 | +## Tuples |
| 4 | + |
| 5 | +A _tuple_ is a general way of grouping together a number of values with a variety of types into one compound type. Tuples have a fixed length: once declared, they cannot grow or shrink in size. |
| 6 | + |
| 7 | +We create a tuple by writing a comma-separated list of values inside parentheses. Each position in the tuple has a type, and the types of the different values in the tuple don’t have to be the same. |
| 8 | +We’ve added optional type annotations in this example: |
| 9 | + |
| 10 | +```rust |
| 11 | +let my_tuple: (i32, f64, u8) = (500, 6.4, 1); |
| 12 | +``` |
| 13 | + |
| 14 | +The variable `my_tuple` binds to the entire tuple because a tuple is considered |
| 15 | +a single compound element. |
| 16 | +To get the individual values out of a tuple, we can use pattern matching to |
| 17 | +destructure a tuple value, like this: |
| 18 | + |
| 19 | +```rust |
| 20 | +let (x, y, z) = my_tuple; |
| 21 | + |
| 22 | +println!("{}", y); |
| 23 | +// => 6.4 |
| 24 | + |
| 25 | +``` |
| 26 | + |
| 27 | +This program first creates a tuple and binds it to the variable `my_tuple`. |
| 28 | +It then uses a pattern with let to take `my_tuple` and turn it into three separate variables, `x`, `y`, and `z`. |
| 29 | +This is called _destructuring_ because it breaks the single tuple into three |
| 30 | +parts. |
| 31 | +Finally, the program prints the value of `y`, which is `6.4`. |
| 32 | + |
| 33 | +Sometimes, when _destructuring_ a tuple, some values might not be important or |
| 34 | +needed, these can be discarded by labeling them with "`_`" (underscore). |
| 35 | + |
| 36 | +```rust |
| 37 | +let (_, y, _) = my_tuple; |
| 38 | + |
| 39 | +println!("{}", y); |
| 40 | +// => 6.4 |
| 41 | + |
| 42 | +``` |
| 43 | + |
| 44 | +We can also access a tuple element directly by using a period (.) followed by the index of the value we want to access. |
| 45 | +For example: |
| 46 | + |
| 47 | +```rust |
| 48 | +let my_tuple: (i32, f64, u8) = (500, 6.4, 1); |
| 49 | + |
| 50 | +let five_hundred = my_tuple.0; |
| 51 | + |
| 52 | +let six_point_four = my_tuple.1; |
| 53 | + |
| 54 | +let one = my_tuple.2; |
| 55 | +``` |
| 56 | + |
| 57 | +This program creates the tuple `my_tuple` and then accesses each of its elements using their respective indices. |
| 58 | +As with most programming languages, the first index in a tuple is 0. |
| 59 | + |
| 60 | +A tuple can contain 0, or upto 12 elements. A tuple with zero elements has a |
| 61 | +special name, _unit_. |
| 62 | + |
| 63 | +```rust |
| 64 | +let my_zero_tuple = (); |
| 65 | +let my_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); |
| 66 | +``` |
| 67 | + |
| 68 | +### Calling methods |
| 69 | +One can call any methods on a value held by a tuple by either first destructuring that value out of the tuple or accessing it using it's index. |
| 70 | + |
| 71 | +```rust |
| 72 | +let my_tuple = (12, "hello"); |
| 73 | + |
| 74 | +let (my_num, my_str) = my_tuple; |
| 75 | + |
| 76 | +my_str.to_uppercase(); |
| 77 | + |
| 78 | +// OR |
| 79 | + |
| 80 | +my_tuple.1.to_uppercase(); |
| 81 | +``` |
| 82 | + |
| 83 | +### Functions can accept a tuple as a parameter. |
| 84 | +Accepting a tuple as a parameter requires to explicitly define its type. The |
| 85 | +following example illustrates that. |
| 86 | + |
| 87 | +```rust |
| 88 | +fn my_function(my_tuple: (i32, &str)) { |
| 89 | + // Do something with my_tuple |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +### Functions can return tuple as a result. |
| 94 | +Returning a tuple as a result requires to explicitly define its type. The |
| 95 | +following example illustrates that. |
| 96 | + |
| 97 | +```rust |
| 98 | +fn make_tuple(an_int: i32, a_string: &str) -> (i32, &str) { |
| 99 | + return (an_int, a_string); |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### Methods can return tuple as a result. |
| 104 | +Methods on various types sometimes return a tuple as a result. Consider the |
| 105 | +following example of a `&str` variable's `.split_at()` method. |
| 106 | + |
| 107 | +```rust |
| 108 | +let str = "Per Martin-Löf"; |
| 109 | + |
| 110 | +let (first, last) = str.split_at(3); |
| 111 | + |
| 112 | +first // => "Per" |
| 113 | +last // => " Martin-Löf" |
| 114 | +``` |
0 commit comments