box-o-sand/rustbyexample/01221/src/main.rs

24 lines
460 B
Rust
Raw Normal View History

2016-08-31 15:18:25 +00:00
use std::fmt;
struct List(Vec<i32>);
impl fmt::Display for List {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let List(ref vec) = *self;
try!(write!(f, "["));
for (count, v) in vec.iter().enumerate() {
if count != 0 { try!(write!(f, ", ")); }
2016-08-31 15:19:13 +00:00
try!(write!(f, "{}: {}", count, v));
2016-08-31 15:18:25 +00:00
}
write!(f, "]")
}
}
fn main() {
let v = List(vec![1, 2, 3]);
println!("{}", v);
}