Skip to content

Instantly share code, notes, and snippets.

@SiegeLord
Last active November 2, 2019 18:59
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 SiegeLord/11456760237781442cfe to your computer and use it in GitHub Desktop.
Save SiegeLord/11456760237781442cfe to your computer and use it in GitHub Desktop.
Rust matrix moving
clone
move
move
Matrix { data: [100, 200, 300] }
#[deriving(Show, Clone)]
struct Matrix
{
data: Vec<f32>
}
trait MoveMul<T, RHS>
{
fn move_mul(self, r: RHS) -> T;
}
impl MoveMul<Matrix, f32> for Matrix
{
fn move_mul(self, r: f32) -> Matrix
{
println!("move");
let mut m = self;
for v in m.data.mut_iter()
{
*v *= r;
}
m
}
}
impl<'l> MoveMul<Matrix, f32> for &'l Matrix
{
fn move_mul(self, r: f32) -> Matrix
{
println!("clone");
let mut m = self.clone();
for v in m.data.mut_iter()
{
*v *= r;
}
m
}
}
fn main()
{
let m = Matrix{ data: vec![1.0f32, 2.0, 3.0] };
let m2 = (&m).move_mul(2.0).move_mul(5.0).move_mul(10.0);
println!("{}", m2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment