Testcase: List

Implementing fmt::Display for a structure where the elements must each be
handled sequentially is tricky. The problem is that each write! generates a
fmt::Result. Proper handling of this requires dealing with all the
results. Rust provides the ? operator for exactly this purpose.

Using ? on write! looks like this:

  1. // Try `write!` to see if it errors. If it errors, return
  2. // the error. Otherwise continue.
  3. write!(f, "{}", value)?;

Alternatively, you can also use the try! macro, which works the same way.
This is a bit more verbose and no longer recommended, but you may still see it in
older Rust code. Using try! looks like this:

  1. try!(write!(f, "{}", value));

With ? available, implementing fmt::Display for a Vec is
straightforward:

  1. use std::fmt; // Import the `fmt` module.
  2. // Define a structure named `List` containing a `Vec`.
  3. struct List(Vec<i32>);
  4. impl fmt::Display for List {
  5. fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
  6. // Extract the value using tuple indexing
  7. // and create a reference to `vec`.
  8. let vec = &self.0;
  9. write!(f, "[")?;
  10. // Iterate over `vec` in `v` while enumerating the iteration
  11. // count in `count`.
  12. for (count, v) in vec.iter().enumerate() {
  13. // For every element except the first, add a comma.
  14. // Use the ? operator, or try!, to return on errors.
  15. if count != 0 { write!(f, ", ")?; }
  16. write!(f, "{}", v)?;
  17. }
  18. // Close the opened bracket and return a fmt::Result value
  19. write!(f, "]")
  20. }
  21. }
  22. fn main() {
  23. let v = List(vec![1, 2, 3]);
  24. println!("{}", v);
  25. }

Activity

Try changing the program so that the index of each element in the vector is also printed. The new output should look like this:

  1. [0: 1, 1: 2, 2: 3]

See also

for, ref, Result, struct,
?, and vec!