Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Define a structure named `List` containing a `Vec`.
- use std::fmt;
- use std::ops::Deref;
- struct List(Vec<i32>);
- impl Deref for List {
- type Target = Vec<i32>;
- fn deref(&self) -> &Vec<i32> {
- &self.0
- }
- }
- 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, "{}", v));
- }
- // Close the opened bracket and return a fmt::Result value
- write!(f, "]")
- }
- }
- fn main() {
- let v = List(vec![1, 2, 3]);
- println!("{:?}", v.len());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement