summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJustin Worthe <justin.worthe@gmail.com>2016-11-26 12:07:11 +0200
committerJustin Worthe <justin.worthe@gmail.com>2016-11-26 12:07:11 +0200
commit4cc968489eb0acd9b5be7e1cdb06bdb8f300f102 (patch)
tree3415abc5d569eab8cec9a17e57b39fe8d5fc85b1
parent3e257573af99b99ca3c710e29c11a1798cd90f31 (diff)
Complex number with addition
-rw-r--r--src/lib.rs29
1 files changed, 28 insertions, 1 deletions
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<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> std::ops::Add for Complex<T> where T: std::ops::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 it_works() {
+ fn addition() {
+ let a = Complex::new(1, 5);
+ let b = Complex::new(-3, 2);
+ assert_eq!(a+b, Complex::new(-2, 7));
}
}
+