Commit 18b0a5420234ef9b723ca24dde1b8f54bb6f2944

Authored by Georg Hopp
1 parent 31aa7b72

Add Display trait for Vector

@@ -130,22 +130,22 @@ fn _vector() { @@ -130,22 +130,22 @@ fn _vector() {
130 let v2 = Vector(2.into(), 2.into(), 3.into()); 130 let v2 = Vector(2.into(), 2.into(), 3.into());
131 let s :Fractional = 3.into(); 131 let s :Fractional = 3.into();
132 132
133 - println!("{:>14} : {:?}", "Vector v1", v1);  
134 - println!("{:>14} : {:?}", "Vector v2", v2);  
135 - println!("{:>14} : {}" , "abs v1", v1.abs());  
136 - println!("{:>14} : {:?}", "-v1", -v1);  
137 - println!("{:>14} : {:?}", "v1 + v1", v1 + v1);  
138 - println!("{:>14} : {:?}", "v1 - v1", v1 - v1);  
139 - println!("{:>14} : {:?}", "v2 - v1", v2 - v1);  
140 - println!("{:>14} : {:?}", format!("v1 * {}", s), v1.mul(&s));  
141 - println!("{:>14} : {:?}", "norm v1", v1.norm());  
142 - println!("{:>14} : {}" , "abs norm v1", v1.norm().abs());  
143 - println!("{:>14} : {}" , "distance v1 v2", v1.distance(v2));  
144 - println!("{:>14} : {}" , "distance v2 v1", v2.distance(v1));  
145 - println!("{:>14} : {}" , "v1 dot v2", v1.dot(v2));  
146 - println!("{:>14} : {}" , "v2 dot v1", v2.dot(v1));  
147 - println!("{:>14} : {:?}", "v1 * v2", v1 * v2);  
148 - println!("{:>14} : {:?}", "v2 * v1", v2 * v1); 133 + println!("{:>14} : {}", "Vector v1", v1);
  134 + println!("{:>14} : {}", "Vector v2", v2);
  135 + println!("{:>14} : {}", "abs v1", v1.abs());
  136 + println!("{:>14} : {}", "-v1", -v1);
  137 + println!("{:>14} : {}", "v1 + v1", v1 + v1);
  138 + println!("{:>14} : {}", "v1 - v1", v1 - v1);
  139 + println!("{:>14} : {}", "v2 - v1", v2 - v1);
  140 + println!("{:>14} : {}", format!("v1 * {}", s), v1.mul(&s));
  141 + println!("{:>14} : {}", "norm v1", v1.norm());
  142 + println!("{:>14} : {}", "abs norm v1", v1.norm().abs());
  143 + println!("{:>14} : {}", "distance v1 v2", v1.distance(v2));
  144 + println!("{:>14} : {}", "distance v2 v1", v2.distance(v1));
  145 + println!("{:>14} : {}", "v1 dot v2", v1.dot(v2));
  146 + println!("{:>14} : {}", "v2 dot v1", v2.dot(v1));
  147 + println!("{:>14} : {}", "v1 * v2", v1 * v2);
  148 + println!("{:>14} : {}", "v2 * v1", v2 * v1);
149 } 149 }
150 150
151 fn main() { 151 fn main() {
@@ -19,6 +19,7 @@ @@ -19,6 +19,7 @@
19 // along with this program. If not, see <http://www.gnu.org/licenses/>. 19 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 // 20 //
21 use std::ops::{Add, Sub, Neg, Mul}; 21 use std::ops::{Add, Sub, Neg, Mul};
  22 +use std::fmt;
22 use crate::{Fractional}; 23 use crate::{Fractional};
23 24
24 #[derive(Debug, Eq, Clone, Copy)] 25 #[derive(Debug, Eq, Clone, Copy)]
@@ -58,6 +59,12 @@ impl Vector { @@ -58,6 +59,12 @@ impl Vector {
58 } 59 }
59 } 60 }
60 61
  62 +impl fmt::Display for Vector {
  63 + fn fmt(&self, f :&mut fmt::Formatter<'_>) -> fmt::Result {
  64 + write!(f, "({}, {}, {})", self.0, self.1, self.2)
  65 + }
  66 +}
  67 +
61 impl PartialEq for Vector { 68 impl PartialEq for Vector {
62 fn eq(&self, other :&Self) -> bool { 69 fn eq(&self, other :&Self) -> bool {
63 let Vector(x1, y1, z1) = self; 70 let Vector(x1, y1, z1) = self;
Please register or login to post a comment