Last active
November 2, 2019 18:59
-
-
Save SiegeLord/11456760237781442cfe to your computer and use it in GitHub Desktop.
Rust matrix moving
This file contains hidden or 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
clone | |
move | |
move | |
Matrix { data: [100, 200, 300] } |
This file contains hidden or 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
#[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