#![allow(unused_mut)] #![allow(unused_variables)] use std::ops::{Add, AddAssign,Neg}; //PartialEq-> a==b #[derive(Debug)] struct Complex { re:T, im:T } impl Complex { fn new(re:T,im:T)->Complex{ Complex::{re,im} } } impl Add for Complex where T:Add { type Output=Complex; //a+b fn add(self,rhs:Self)->Self::Output { Complex{ re:self.re+rhs.re, im:self.im+rhs.im } } } impl AddAssign for Complex where T:AddAssign{ fn add_assign(&mut self,rhs:Self) { self.re+= rhs.re; self.im+=rhs.im; } } impl Neg for Complex where T:Neg { type Output = Complex; fn neg(self) -> Self::Output { Complex{ re:-self.re, im:-self.im } } } impl PartialEq for Complex where T:PartialEq{ fn eq(&self, rhs: &Self) -> bool { self.re==rhs.re && self.im==rhs.im } } fn main() { let mut a=Complex::new(1,2); let mut b=Complex::new(8,4); //println!("{:?}",b); //println!("{:?}",a+b); //a+=b; //println!("a={:?}",-a); println!("{:?}",a!=b); }