Advertisement
cwchen

[Rust] Implementing fmt::Debug trait for Vector class.

Sep 14th, 2017
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.49 KB | None | 0 0
  1. // Overloaded debug string
  2. impl<T> fmt::Debug for Vector<T> where T: Copy + fmt::Display + num::Num {
  3.     fn fmt(&self, f:&mut fmt::Formatter) -> fmt::Result {
  4.         let mut s = String::new();
  5.  
  6.         s += "[";
  7.  
  8.         for i in 0..(self.vec.len()) {
  9.             s += &format!("{}", self.vec[i]);
  10.  
  11.             if i < self.vec.len() - 1 {
  12.                 s += ", ";
  13.             }
  14.         }
  15.  
  16.         s += "]";
  17.  
  18.         // Write string to formatter
  19.         write!(f, "{}", s)
  20.     }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement