]> git.proxmox.com Git - rustc.git/blob - src/doc/rust-by-example/src/primitives/tuples.md
New upstream version 1.62.1+dfsg1
[rustc.git] / src / doc / rust-by-example / src / primitives / tuples.md
1 # Tuples
2
3 A tuple is a collection of values of different types. Tuples are constructed
4 using parentheses `()`, and each tuple itself is a value with type signature
5 `(T1, T2, ...)`, where `T1`, `T2` are the types of its members. Functions can
6 use tuples to return multiple values, as tuples can hold any number of values.
7
8 ```rust,editable
9 // Tuples can be used as function arguments and as return values
10 fn reverse(pair: (i32, bool)) -> (bool, i32) {
11 // `let` can be used to bind the members of a tuple to variables
12 let (integer, boolean) = pair;
13
14 (boolean, integer)
15 }
16
17 // The following struct is for the activity.
18 #[derive(Debug)]
19 struct Matrix(f32, f32, f32, f32);
20
21 fn main() {
22 // A tuple with a bunch of different types
23 let long_tuple = (1u8, 2u16, 3u32, 4u64,
24 -1i8, -2i16, -3i32, -4i64,
25 0.1f32, 0.2f64,
26 'a', true);
27
28 // Values can be extracted from the tuple using tuple indexing
29 println!("long tuple first value: {}", long_tuple.0);
30 println!("long tuple second value: {}", long_tuple.1);
31
32 // Tuples can be tuple members
33 let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
34
35 // Tuples are printable
36 println!("tuple of tuples: {:?}", tuple_of_tuples);
37
38 // But long Tuples (more than 12 elements) cannot be printed
39 // let too_long_tuple = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);
40 // println!("too long tuple: {:?}", too_long_tuple);
41 // TODO ^ Uncomment the above 2 lines to see the compiler error
42
43 let pair = (1, true);
44 println!("pair is {:?}", pair);
45
46 println!("the reversed pair is {:?}", reverse(pair));
47
48 // To create one element tuples, the comma is required to tell them apart
49 // from a literal surrounded by parentheses
50 println!("one element tuple: {:?}", (5u32,));
51 println!("just an integer: {:?}", (5u32));
52
53 //tuples can be destructured to create bindings
54 let tuple = (1, "hello", 4.5, true);
55
56 let (a, b, c, d) = tuple;
57 println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d);
58
59 let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
60 println!("{:?}", matrix);
61
62 }
63 ```
64
65 ### Activity
66
67 1. *Recap*: Add the `fmt::Display` trait to the `Matrix` struct in the above example,
68 so that if you switch from printing the debug format `{:?}` to the display
69 format `{}`, you see the following output:
70
71 ```text
72 ( 1.1 1.2 )
73 ( 2.1 2.2 )
74 ```
75
76 You may want to refer back to the example for [print display][print_display].
77 2. Add a `transpose` function using the `reverse` function as a template, which
78 accepts a matrix as an argument, and returns a matrix in which two elements
79 have been swapped. For example:
80
81 ```rust,ignore
82 println!("Matrix:\n{}", matrix);
83 println!("Transpose:\n{}", transpose(matrix));
84 ```
85
86 results in the output:
87
88 ```text
89 Matrix:
90 ( 1.1 1.2 )
91 ( 2.1 2.2 )
92 Transpose:
93 ( 1.1 2.1 )
94 ( 1.2 2.2 )
95 ```
96
97 [print_display]: ../hello/print/print_display.md