Tuples
A tuple is a collection of values of different types. Tuples are constructed
using parentheses ()
, and each tuple itself is a value with type signature
(T1, T2, ...)
, where T1
, T2
are the types of its members. Functions can
use tuples to return multiple values, as tuples can hold any number of values.
Activity
-
Recap: Add the
fmt::Display
trait to theMatrix
struct in the above example, so that if you switch from printing the debug format{:?}
to the display format{}
, you see the following output:( 1.1 1.2 ) ( 2.1 2.2 )
You may want to refer back to the example for print display.
-
Add a
transpose
function using thereverse
function as a template, which accepts a matrix as an argument, and returns a matrix in which two elements have been swapped. For example:println!("Matrix:\n{}", matrix); println!("Transpose:\n{}", transpose(matrix));
Results in the output:
Matrix: ( 1.1 1.2 ) ( 2.1 2.2 ) Transpose: ( 1.1 2.1 ) ( 1.2 2.2 )