@@ -6,9 +6,9 @@ using parentheses `()`, and each tuple itself is a value with type signature
6
6
use tuples to return multiple values, as tuples can hold any number of values.
7
7
8
8
``` rust,editable
9
- // Tuples can be used as function arguments and as return values
9
+ // Tuples can be used as function arguments and as return values.
10
10
fn reverse(pair: (i32, bool)) -> (bool, i32) {
11
- // `let` can be used to bind the members of a tuple to variables
11
+ // `let` can be used to bind the members of a tuple to variables.
12
12
let (int_param, bool_param) = pair;
13
13
14
14
(bool_param, int_param)
@@ -19,79 +19,78 @@ fn reverse(pair: (i32, bool)) -> (bool, i32) {
19
19
struct Matrix(f32, f32, f32, f32);
20
20
21
21
fn main() {
22
- // A tuple with a bunch of different types
22
+ // A tuple with a bunch of different types.
23
23
let long_tuple = (1u8, 2u16, 3u32, 4u64,
24
24
-1i8, -2i16, -3i32, -4i64,
25
25
0.1f32, 0.2f64,
26
26
'a', true);
27
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);
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
31
32
- // Tuples can be tuple members
32
+ // Tuples can be tuple members.
33
33
let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
34
34
35
- // Tuples are printable
35
+ // Tuples are printable.
36
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);
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
41
// TODO ^ Uncomment the above 2 lines to see the compiler error
42
42
43
43
let pair = (1, true);
44
- println!("pair is {:?}", pair);
44
+ println!("Pair is {:?}", pair);
45
45
46
- println!("the reversed pair is {:?}", reverse(pair));
46
+ println!("Uhe reversed pair is {:?}", reverse(pair));
47
47
48
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));
49
+ // from a literal surrounded by parentheses.
50
+ println!("One element tuple: {:?}", (5u32,));
51
+ println!("Just an integer: {:?}", (5u32));
52
52
53
- //tuples can be destructured to create bindings
53
+ // Tuples can be destructured to create bindings.
54
54
let tuple = (1, "hello", 4.5, true);
55
55
56
56
let (a, b, c, d) = tuple;
57
57
println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d);
58
58
59
59
let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
60
60
println!("{:?}", matrix);
61
-
62
61
}
63
62
```
64
63
65
64
### Activity
66
65
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
- ```
66
+ 1 . * Recap* : Add the ` fmt::Display ` trait to the ` Matrix ` struct in the above
67
+ example, so that if you switch from printing the debug format ` {:?} ` to the
68
+ display format ` {} ` , you see the following output:
69
+
70
+ ``` text
71
+ ( 1.1 1.2 )
72
+ ( 2.1 2.2 )
73
+ ```
74
+
75
+ You may want to refer back to the example for [ print display] [ print_display ] .
76
+ 2 . Add a ` transpose ` function using the ` reverse ` function as a template, which
77
+ accepts a matrix as an argument, and returns a matrix in which two elements
78
+ have been swapped. For example:
79
+
80
+ ``` rust,ignore
81
+ println!("Matrix:\n{}", matrix);
82
+ println!("Transpose:\n{}", transpose(matrix));
83
+ ```
84
+
85
+ Results in the output:
86
+
87
+ ``` text
88
+ Matrix:
89
+ ( 1.1 1.2 )
90
+ ( 2.1 2.2 )
91
+ Transpose:
92
+ ( 1.1 2.1 )
93
+ ( 1.2 2.2 )
94
+ ```
96
95
97
96
[ print_display ] : ../hello/print/print_display.md
0 commit comments