Last active
August 29, 2015 14:24
-
-
Save HybridEidolon/2edb6f852356eeb23cc5 to your computer and use it in GitHub Desktop.
rust operator overloading
This file contains 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::Add; | |
#[derive(Copy, Clone)] | |
struct MyStruct { | |
pub a: i32 | |
} | |
impl Add for MyStruct { | |
type Output = MyStruct; | |
fn add(self, rhs: MyStruct) -> MyStruct { | |
println!("i did a thing"); | |
MyStruct { a: self.a + rhs.a } | |
} | |
} | |
fn main() { | |
let s = MyStruct { a: 4 }; | |
let a = s + s; | |
println!("{}", a.a); | |
} | |
// i did a thing | |
// 8 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment