Skip to content

Instantly share code, notes, and snippets.

@hosszukalman
Created December 5, 2016 08:42
Show Gist options
  • Save hosszukalman/e5482f392b54cd57ee2a1d2fad3b2383 to your computer and use it in GitHub Desktop.
Save hosszukalman/e5482f392b54cd57ee2a1d2fad3b2383 to your computer and use it in GitHub Desktop.
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