# Print a HashMap in Rust

> Print a Rust HashMap with {:?} or {:#?}, understand why entry order can change, and choose deterministic output when order matters.

HTML: https://rustprint.com/debug/print-hashmap/

Use `{:?}` to print a `HashMap` for debugging. With a captured variable, write `println!("{scores:?}")`. Use `{:#?}` when a multi-line representation is easier to scan.

**Print a HashMap with compact and pretty Debug**

```rust
let scores = HashMap::from([("Blue", 10), ("Red", 20)]);
println!("{scores:?}");
println!("{scores:#?}");
```

RustPrint executes this example in the Rust test suite. The test checks that the formatted map contains the expected keys and values without asserting their order.

## HashMap supports Debug when its keys and values do

The standard library implements `Debug` for `HashMap<K, V>` when both `K` and `V` implement `Debug`. That is why collections such as `HashMap<&str, i32>` can be inspected directly with `{:?}`.

If a custom key or value type does not implement `Debug`, derive or implement `Debug` for that type first. See [E0277: type doesn't implement Debug](/errors/doesnt-implement-debug/index.md) for the checked failure case.

## Do not depend on the printed entry order

A `HashMap` does not promise a stable iteration order. The standard library uses randomized hashing by default, and Rust documentation explicitly treats map iteration as arbitrary order.

That means these two outputs can represent the same logical map:

```text
{"Blue": 10, "Red": 20}
{"Red": 20, "Blue": 10}
```

For debugging, that is normally fine. For tests, files, snapshots, generated text, or any output where order matters, do not compare the raw Debug string.

## Pretty-print a HashMap

`{:#?}` asks the `Debug` implementation for its alternate pretty representation. This usually makes larger maps easier to read by placing entries across multiple indented lines.

Pretty Debug is still inspection output. Rust does not guarantee its exact textual representation as a serialization format or stable interface.

## When you need deterministic output

If the order is part of the output contract, choose it explicitly. One option is to collect the entries or keys, sort them, and print them in that order. Another is to use an ordered map type when ordered traversal is part of the data-structure requirement.

If you only need quick inspection, keep it simple and use `{:?}` or `{:#?}`. For the broader representation choice, see [Debug vs Display](/debug/debug-vs-display/index.md).

## Sources

- [std::collections::HashMap](https://doc.rust-lang.org/std/collections/struct.HashMap.html)
- [std::fmt::Debug](https://doc.rust-lang.org/std/fmt/trait.Debug.html)

## Related RustPrint guides

- [Print a Vec in Rust](/debug/print-vec/index.md): Print a Rust Vec with {:?} or {:#?}, understand why {} fails, and format vector elements yourself when you need user-facing output.
- [Print structs and arrays in Rust](/debug/print-struct/index.md): Print a Rust struct by deriving Debug and using {:?} or {:#?}, and print arrays with the same Debug formatting syntax.
- [Rust pretty print a struct with {:#?}](/debug/pretty-debug/index.md): Pretty print Rust structs, enums, maps, and nested values with {:#?}, derive Debug, and understand why Debug output is not a stable serialization format.
- [Debug vs Display in Rust](/debug/debug-vs-display/index.md): Choose between Rust Debug and Display based on programmer-facing inspection versus deliberate user-facing text.
