Software Development

Rust – Display Contents of Array, Tuple, HashMap and Vector

Photo by Mike from Pexels

This post is about displaying the contents of Arrays, Tuples, HashMaps and Vectors in Rust.

Debug Trait

These three compound types, by default, implement the Debug trait. We use the following display formatters with println!  or print! functions to display their contents.

  • {:?}
    • single-line display
  • {:#?}
    • pretty-print alternative

For struct instances, please check out How to Display the Contents of Struct

Examples

Arrays

Output

Tuples

Output

HashMaps

Output

Vectors

Output

Complete Codes

These codes are a bit different as we pass our Vec and HashMap instances into my_tuple.

Output

Test with Rust 1.37.0.

You Might Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *