Created
December 5, 2016 08:42
-
-
Save hosszukalman/e5482f392b54cd57ee2a1d2fad3b2383 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct List(Vec<i32>); | |
impl fmt::Display for List { | |
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
// Dereference `self` and create a reference to `vec` | |
// via destructuring. | |
let List(ref vec) = *self; | |
try!(write!(f, "[")); | |
// Iterate over `vec` in `v` while enumerating the iteration | |
// count in `count`. | |
for (count, v) in vec.iter().enumerate() { | |
// For every element except the first, add a comma | |
// before calling `write!`. Use `try!` to return on errors. | |
if count != 0 { | |
try!(write!(f, ", ")); | |
} | |
try!(write!(f, "{}: {}", count, v)); | |
} | |
// Close the opened bracket and return a fmt::Result value | |
write!(f, "]") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment