When using collection types, we sometimes do not want to use loops to display each value. For example, in Rust, we can display the content of an Array, Tuple, HashMap (Map), or Vector without loops using the Debug trait.
Rust Debug Trait
These three compound types, by default, implement the Debug trait. Then, 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
Rust Codes To Display Content of Array
Consider the following array of string values.
1 2 3 4 | let my_array = ["It's", "amazing!", "With", "the", "blink", "of", "an", "eye", "you", "finally", "see", "the", "light"]; |
To display the values without loops, we use these codes:
1 2 | println!("my_array -> {:?}", my_array); println!("my_array -> {:#?}", my_array); |
Then, we get the output as follows.
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", ] |
Rust Codes To Display Content of Tuple
Here are sample codes to display the content of a tuple in Rust. Remember, a tuple is like an array, but it can hold values of different types, and it uses parentheses instead of square brackets.
1 2 3 4 | let my_tuple = (1, "Hello", 'A'); println!("my_tuple -> {:?}", my_tuple); println!("my_tuple -> {:#?}", my_tuple); |
When we run the codes, we get this result:
1 2 3 4 5 6 | my_tuple -> (1, "Hello", 'A') my_tuple -> ( 1, "Hello", 'A', ) |
Rust Codes To Display Content of HashMap (Map)
With HashMap (or Map), we need a little bit more code when creating test data. Creating an instance of HashMap is not enough.
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); |
When the codes run, we get the following output.
1 2 3 4 5 | my_map -> {"001": "Kevin", "002": "Mark"} my_map -> { "001": "Kevin", "002": "Mark", } |
Rust Codes To Display Content of Vector
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. Alternatively, we could use the vec! macro to initialize a Vector.
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", }, ) |
At this point, we can see that the codes to display the content of an Array, a HashMap (Map), a Tuple, or a Vector are quite similar. We can attribute this benefit to the use of the Debug trait. Therefore, when we have custom compound types or even struct types, we could use the Debug trait to conveniently display their instances’ content with just one line of code. As a result, our Rust codes become more compact.
We tested the codes using Rust 1.52.1.