@@ -6,9 +6,9 @@ using parentheses `()`, and each tuple itself is a value with type signature
66use tuples to return multiple values, as tuples can hold any number of values.
77
88``` 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.
1010fn 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.
1212 let (int_param, bool_param) = pair;
1313
1414 (bool_param, int_param)
@@ -19,79 +19,78 @@ fn reverse(pair: (i32, bool)) -> (bool, i32) {
1919struct Matrix(f32, f32, f32, f32);
2020
2121fn main() {
22- // A tuple with a bunch of different types
22+ // A tuple with a bunch of different types.
2323 let long_tuple = (1u8, 2u16, 3u32, 4u64,
2424 -1i8, -2i16, -3i32, -4i64,
2525 0.1f32, 0.2f64,
2626 'a', true);
2727
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);
3131
32- // Tuples can be tuple members
32+ // Tuples can be tuple members.
3333 let tuple_of_tuples = ((1u8, 2u16, 2u32), (4u64, -1i8), -2i16);
3434
35- // Tuples are printable
35+ // Tuples are printable.
3636 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);
4141 // TODO ^ Uncomment the above 2 lines to see the compiler error
4242
4343 let pair = (1, true);
44- println!("pair is {:?}", pair);
44+ println!("Pair is {:?}", pair);
4545
46- println!("the reversed pair is {:?}", reverse(pair));
46+ println!("Uhe reversed pair is {:?}", reverse(pair));
4747
4848 // 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));
5252
53- //tuples can be destructured to create bindings
53+ // Tuples can be destructured to create bindings.
5454 let tuple = (1, "hello", 4.5, true);
5555
5656 let (a, b, c, d) = tuple;
5757 println!("{:?}, {:?}, {:?}, {:?}", a, b, c, d);
5858
5959 let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
6060 println!("{:?}", matrix);
61-
6261}
6362```
6463
6564### Activity
6665
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+ ```
9695
9796[ print_display ] : ../hello/print/print_display.md
0 commit comments