Advertisement
Guest User

Untitled

a guest
Mar 30th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.63 KB | None | 0 0
  1. use std::ops::Add;
  2.  
  3. struct MyStruct(i32);
  4. struct MyStructString(String);
  5.  
  6. impl Add for MyStruct {
  7.     type Output = MyStruct;
  8.  
  9.     fn add(self, other: MyStruct) -> MyStruct {
  10.         MyStruct(self.0 + other.0)
  11.     }
  12. }
  13.  
  14. impl Add for MyStructString {
  15.     type Output = MyStructString;
  16.  
  17.     fn add(self, other: MyStructString) -> MyStructString {
  18.         MyStructString(format!("{}{}", self.0, other.0))
  19.     }
  20. }
  21.  
  22. fn main() {
  23.     let a = MyStruct(1);
  24.     let b = MyStruct(2);
  25.     let _c = a + b;
  26.    
  27.     let aa = MyStructString(String::from("A"));
  28.     let bb = MyStructString(String::from("B"));
  29.     let _cc = aa + bb;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement