{}, {:?}, v, vv, vvv, verbosity

Rust provides defaults of:
"{}" = default
"{:?}" = debug

I am wondering if there is a standard way to print out structs in various levels of verbosity. For example, consider a``x: HashMap<K, Vec>`,

  • at one level, we might want to just say HashMap<K, Vec<T>>,
  • at another level, we might want to print out for every (k, v) (k, v.len())
  • yet on another level, we might want to print out every element of the Vector too

The context here is for debugging. For certain structures, if we just dump it out via Debug, it takes too up too much space and becomes line noise. Is there a standard best practice for dealing with this problem ?

[Key issue here: there is no single one right answer; depending on context, we want either summary or verbose; the question is then how to easily switch between them.]

Create newtype wrappers for each required representation and implement Debug for them. Then instead of changing the format string, you would change the wrapper type. (Or you can create a single wrapper with a dynamically-changing enum Verbosity.)

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.