Created
August 28, 2016 18:27
-
-
Save greyblake/55b69f4bac29071f6355566339a0a2ec to your computer and use it in GitHub Desktop.
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
use std::ops::Mul; | |
#[derive(Debug)] | |
struct Vector { | |
x : f64, | |
y : f64 | |
} | |
impl<'a> Mul<f64> for &'a Vector { | |
type Output = Vector; | |
fn mul(self, k : f64) -> Vector { | |
Vector { x: self.x * k, y: self.y * k } | |
} | |
} | |
impl<'a> Mul<i32> for &'a Vector { | |
type Output = Vector; | |
fn mul(self, k : i32) -> Vector { | |
let k = k as f64; | |
Vector { x: self.x * k, y: self.y * k } | |
} | |
} | |
fn main() { | |
let v = Vector { x: 1.0, y: 2.0 }; | |
let v2 = &v * 1.5; | |
let v3 = &v * 3; | |
println!("{:?}", v2); | |
println!("{:?}", v3); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you don't mind of copy things a bit.