Last active
November 16, 2017 03:07
-
-
Save tolitius/7e96f08d6cbc1362e745dee2f8595e3b to your computer and use it in GitHub Desktop.
rust ((by example): tuples) -> https://rustbyexample.com/primitives/tuples.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::fmt::{self, Formatter, Display}; | |
struct Matrix(f32, f32, f32, f32); | |
// do we prefer pure or since &mut is safe (and faster) it is preferable? | |
fn transpose(Matrix(a, b, c, d): Matrix) -> Matrix { | |
Matrix(a, c, b, d) | |
} | |
impl Display for Matrix { | |
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | |
// I liike it, is it how it is usually done? | |
let Matrix(a, b, c, d) = *self; | |
write!(f, "( {} {} )\n", a, b)?; | |
write!(f, "( {} {} )", c, d) | |
} | |
} | |
fn main() { | |
let matrix = Matrix(1.1, 1.2, 2.1, 2.2); | |
println!("given matrix:\n{}\n", matrix); | |
println!("transposed:\n{}", transpose(matrix)); | |
} | |
/* | |
given matrix: | |
( 1.1 1.2 ) | |
( 2.1 2.2 ) | |
transposed: | |
( 1.1 2.1 ) | |
( 1.2 2.2 ) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment