Skip to content

Instantly share code, notes, and snippets.

@tolitius
Last active November 16, 2017 03:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tolitius/7e96f08d6cbc1362e745dee2f8595e3b to your computer and use it in GitHub Desktop.
Save tolitius/7e96f08d6cbc1362e745dee2f8595e3b to your computer and use it in GitHub Desktop.
rust ((by example): tuples) -> https://rustbyexample.com/primitives/tuples.html
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