diff options
Diffstat (limited to 'src/complex.rs')
-rw-r--r-- | src/complex.rs | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/complex.rs b/src/complex.rs new file mode 100644 index 0000000..0919564 --- /dev/null +++ b/src/complex.rs @@ -0,0 +1,35 @@ +use std::ops::Add; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Complex<T> { + real: T, + imag: T +} + +impl<T: Clone> Complex<T> { + fn new(real: T, imag: T) -> Complex<T> { + Complex{real: real, imag: imag} + } +} + +impl<T> Add for Complex<T> where T: Add<Output=T> + Copy { + type Output = Complex<T>; + + fn add(self, other: Self) -> Self { + let real = self.real + other.real; + let imag = self.imag + other.imag; + Complex::new(real, imag) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn addition() { + let a = Complex::new(1, 5); + let b = Complex::new(-3, 2); + assert_eq!(a+b, Complex::new(-2, 7)); + } +} + |