Skip to content

Instantly share code, notes, and snippets.

@SiegeLord

SiegeLord/output

Last active Nov 2, 2019
Embed
What would you like to do?
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
You can’t perform that action at this time.