Skip to content

Instantly share code, notes, and snippets.

@HybridEidolon
Last active August 29, 2015 14:24
Show Gist options
  • Save HybridEidolon/2edb6f852356eeb23cc5 to your computer and use it in GitHub Desktop.
Save HybridEidolon/2edb6f852356eeb23cc5 to your computer and use it in GitHub Desktop.
rust operator overloading
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