From 4cc968489eb0acd9b5be7e1cdb06bdb8f300f102 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Sat, 26 Nov 2016 12:07:11 +0200 Subject: Complex number with addition --- src/lib.rs | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cdfbe1a..1f29a24 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,33 @@ +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct Complex { + real: T, + imag: T +} + +impl Complex { + fn new(real: T, imag: T) -> Complex { + Complex{real: real, imag: imag} + } +} + +impl std::ops::Add for Complex where T: std::ops::Add + Copy { + type Output = Complex; + + 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 it_works() { + fn addition() { + let a = Complex::new(1, 5); + let b = Complex::new(-3, 2); + assert_eq!(a+b, Complex::new(-2, 7)); } } + -- cgit v1.2.3