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
1 2 3 4 5 6 7 |
let my_array = ["It's", "amazing!", "With", "the", "blink", "of", "an", "eye", "you", "finally", "see", "the", "light"]; println!("my_array -> {:?}", my_array); println!("my_array -> {:#?}", my_array); |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
my_array -> ["It\'s", "amazing!", "With", "the", "blink", "of", "an", "eye", "you", "finally", "see", "the", "light"] my_array -> [ "It\'s", "amazing!", "With", "the", "blink", "of", "an", "eye", "you", "finally", "see", "the", "light", ] |
Tuples
1 2 3 4 |
let my_tuple = (1, "Hello", 'A'); println!("my_tuple -> {:?}", my_tuple); println!("my_tuple -> {:#?}", my_tuple); |
Output
1 2 3 4 5 6 |
my_tuple -> (1, "Hello", 'A') my_tuple -> ( 1, "Hello", 'A', ) |
HashMaps
1 2 3 4 5 6 |
let mut my_map: HashMap<String, String> = HashMap::new(); my_map.insert(String::from("001"), String::from("Kevin")); my_map.insert(String::from("002"), String::from("Mark")); println!("my_map -> {:?}", my_map); println!("my_map -> {:#?}", my_map); |
Output
1 2 3 4 5 |
my_map -> {"001": "Kevin", "002": "Mark"} my_map -> { "001": "Kevin", "002": "Mark", } |
Vectors
1 2 3 4 5 6 7 |
let mut my_vec: Vec<String> = Vec::new(); my_vec.push(String::from("AAA")); my_vec.push(String::from("BBB")); my_vec.push(String::from("CCC")); println!("my_vec -> {:?}", my_vec); println!("my_vec -> {:#?}", my_vec); |
Output
1 2 3 4 5 6 |
my_vec -> ["AAA", "BBB", "CCC"] my_vec -> [ "AAA", "BBB", "CCC", ] |
Complete Codes
These codes are a bit different as we pass our Vec and HashMap instances into my_tuple.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
use std::collections::HashMap; fn main() { let mut my_map: HashMap<String, String> = HashMap::new(); my_map.insert(String::from("001"), String::from("Kevin")); my_map.insert(String::from("002"), String::from("Mark")); let mut my_vec: Vec<String> = Vec::new(); my_vec.push(String::from("AAA")); my_vec.push(String::from("BBB")); my_vec.push(String::from("CCC")); let my_array = ["It's", "amazing!", "With", "the", "blink", "of", "an", "eye", "you", "finally", "see", "the", "light"]; // We pass the reference to mny_vec and my_map to the tuple let my_tuple = (1, "Hello", 'A', &my_vec, &my_map); println!("my_array -> {:?}", my_array); println!("my_array -> {:#?}", my_array); println!("my_map -> {:?}", my_map); println!("my_map -> {:#?}", my_map); println!("my_vec -> {:?}", my_vec); println!("my_vec -> {:#?}", my_vec); println!("my_tuple -> {:?}", my_tuple); println!("my_tuple -> {:#?}", my_tuple); } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
my_array -> ["It\'s", "amazing!", "With", "the", "blink", "of", "an", "eye", "you", "finally", "see", "the", "light"] my_array -> [ "It\'s", "amazing!", "With", "the", "blink", "of", "an", "eye", "you", "finally", "see", "the", "light", ] my_map -> {"002": "Mark", "001": "Kevin"} my_map -> { "002": "Mark", "001": "Kevin", } my_vec -> ["AAA", "BBB", "CCC"] my_vec -> [ "AAA", "BBB", "CCC", ] my_tuple -> (1, "Hello", 'A', ["AAA", "BBB", "CCC"], {"002": "Mark", "001": "Kevin"}) my_tuple -> ( 1, "Hello", 'A', [ "AAA", "BBB", "CCC", ], { "002": "Mark", "001": "Kevin", }, ) |
Test with Rust 1.37.0.