|
| 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. We’ve added optional type annotations in this example: |
| 8 | +```rust |
| 9 | +let my_tuple: (i32, f64, u8) = (500, 6.4, 1); |
| 10 | +``` |
| 11 | + |
| 12 | +The variable `my_tuple` binds to the entire tuple because a tuple is considered |
| 13 | +a single compound element. |
| 14 | +To get the individual values out of a tuple, we can use pattern matching to |
| 15 | +destructure a tuple value, like this: |
| 16 | +```rust |
| 17 | +let (x, y, z) = my_tuple; |
| 18 | +``` |
| 19 | + |
| 20 | +This program first creates a tuple and binds it to the variable tup. It then |
| 21 | +uses a pattern with let to take tup and turn it into three separate variables, |
| 22 | +x, y, and z. |
| 23 | +This is called _destructuring_ because it breaks the single tuple into three parts. Finally, the program prints the value of y, which is 6.4. |
| 24 | + |
| 25 | +We can also access a tuple element directly by using a period (.) followed by the index of the value we want to access. For example: |
| 26 | +```rust |
| 27 | +let my_tuple: (i32, f64, u8) = (500, 6.4, 1); |
| 28 | + |
| 29 | +let five_hundred = my_tuple.0; |
| 30 | + |
| 31 | +let six_point_four = my_tuple.1; |
| 32 | + |
| 33 | +let one = my_tuple.2; |
| 34 | +``` |
| 35 | + |
| 36 | +This program creates the tuple x and then accesses each element of the tuple |
| 37 | +using their respective indices. As with most programming languages, the first |
| 38 | +index in a tuple is 0. |
| 39 | + |
| 40 | +A tuple can contain 0, or upto 12 elements. A tuple with zero elements has a |
| 41 | +special name, _unit_. |
| 42 | +```rust |
| 43 | +let my_zero_tuple = (); |
| 44 | +let my_tuple = (1,2,3,4,5,6,7,8,9,10,11,12) |
| 45 | +``` |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +### Calling methods |
| 50 | +One can call any methods on a value held by a tuple by either first |
| 51 | +destructuring that value out of the tuple or accessing it using it's index. |
| 52 | +```rust |
| 53 | +let my_tuple = (12, "hello"); |
| 54 | + |
| 55 | +let (my_num, my_str) = my_tuple; |
| 56 | + |
| 57 | +my_str.to_uppercase(); |
| 58 | + |
| 59 | +// OR |
| 60 | + |
| 61 | +my_tuple.1.to_uppercase(); |
| 62 | +``` |
| 63 | + |
| 64 | +### Functions can accept a tuple as a parameter. |
| 65 | +Accepting a tuple as a parameter requires to explicitly define its type. The |
| 66 | +following example illustrates that. |
| 67 | +```rust |
| 68 | +fn my_function(my_tuple: (i32, &str)) { |
| 69 | + // Do something with my_tuple |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +### Functions can return tuple as a result. |
| 74 | +Returning a tuple as a result requires to explicitly define its type. The |
| 75 | +following example illustrates that. |
| 76 | +```rust |
| 77 | +fn make_tuple(an_int: i32, a_string: &str) -> (i32, &str) { |
| 78 | + return (an_int, a_string); |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Methods can return tuple as a result. |
| 83 | +Methods on various types sometimes return a tuple as a result. Consider the |
| 84 | +following example of a `&str` variable's `.split_at()` method. |
| 85 | + |
| 86 | +```rust |
| 87 | +let str = "Per Martin-Löf"; |
| 88 | + |
| 89 | +let (first, last) = str.split_at(3); |
| 90 | + |
| 91 | +first // => "Per" |
| 92 | +last // => " Martin-Löf" |
| 93 | +``` |
0 commit comments