# Print structs and arrays in Rust

> Print a Rust struct by deriving Debug and using {:?} or {:#?}, and print arrays with the same Debug formatting syntax.

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

For a struct you own, the usual way to print its fields for debugging is to derive `Debug` and use `{:?}`. With captured identifiers, write `println!("{value:?}")`. Use `{:#?}` for the pretty multi-line form.

**Derive Debug and print a Rust struct**

```rust
#[derive(Debug)]
struct User {
    name: &'static str,
    active: bool,
}

let user = User {
    name: "Ada",
    active: true,
};

println!("{user:?}");
println!("{user:#?}");
```

The checked test verifies that the generated Debug representation contains the struct and field information, without treating its exact textual layout as a stable format.

## Why derive(Debug) is usually enough

`#[derive(Debug)]` asks Rust to generate the `Debug` implementation from the fields of your struct. This works when the fields themselves support `Debug`.

If the compiler says your type does not implement `Debug`, see [E0277: type doesn't implement Debug](/errors/doesnt-implement-debug/index.md).

If the derived representation includes fields you do not want in diagnostic output, [implement custom Debug](/debug/implement-debug/index.md) instead of adding post-processing around the formatted string.

## Pretty-print a struct

Use `{:#?}` instead of `{:?}` when nested fields are hard to read on one line. The alternate Debug flag commonly adds indentation and line breaks.

Pretty Debug is still Debug output. It is intended for inspection and diagnostics, not as a stable serialization or public text format. See [pretty Debug](/debug/pretty-debug/index.md) for that boundary.

## Print an array

Arrays whose elements implement `Debug` can use the same `{:?}` syntax directly:

**Print a Rust array with Debug**

```rust
let numbers = [10, 20, 30];
println!("{numbers:?}");
```

You do not need to iterate over the array just to inspect it while debugging.

## Use Display for deliberate user-facing text

If the struct needs a stable, intentional human-readable representation, implement `Display` and print it with `{}` instead of exposing the derived Debug structure.

See [implement Display for a Rust struct](/format/implement-display/index.md) for a checked implementation and [Debug vs Display](/debug/debug-vs-display/index.md) for choosing between the two traits.

## Sources

- [std::fmt::Debug](https://doc.rust-lang.org/std/fmt/trait.Debug.html)
- [Rust arrays](https://doc.rust-lang.org/std/primitive.array.html)

## Related RustPrint guides

- [Implement custom Debug in Rust](/debug/implement-debug/index.md): Implement Rust's Debug trait manually with Formatter::debug_struct, choose which fields appear, redact sensitive values, and keep {:#?} pretty printing.
- [Print a HashMap in Rust](/debug/print-hashmap/index.md): Print a Rust HashMap with {:?} or {:#?}, understand why entry order can change, and choose deterministic output when order matters.
- [Rust E0277: type doesn't implement Debug](/errors/doesnt-implement-debug/index.md): Fix Rust E0277 when a value cannot be formatted with {:?} because its type does not implement std::fmt::Debug.
- [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.
- [Implement Display for a Rust struct](/format/implement-display/index.md): Implement std::fmt::Display for a Rust struct with Formatter and write!, then use {} formatting and to_string().
