Skip to content

Print a Vec in Rust

Use {:?} to print a Vec for debugging. With captured identifiers, the compact form is println!("{values:?}"). Use {:#?} when a multi-line pretty Debug representation is easier to read.

EXAMPLE: CI-CHECKEDTEST ASSERTED · examples/format-output/src/print_values_pages.rs#debug-print-vec
let values = vec![1, 2, 3];
println!("{values:?}");
println!("{values:#?}");

The example is executed by RustPrint’s Rust test suite. The test checks the Debug representation for the values without treating the exact Debug text as a stable serialization format.

A Vec supports Debug when its elements support Debug, but it does not provide the general-purpose Display representation required by {}.

EXAMPLE: CI-CHECKED FAILURECOMPILE-FAIL ASSERTED · examples/compile-fail/vec-display-not-implemented.rs#vec-display-not-implemented
fn main() {
    let values = vec![1, 2, 3];
    println!("{values}");
}

That failure is compiled intentionally in CI and must continue to produce the expected stable-Rust diagnostic.

If you hit E0277 from {}, see type doesn’t implement Display for the trait-level explanation.

{:?} is convenient for a short vector. {:#?} asks the Debug implementation for its pretty representation and is usually easier to scan when the vector is long or contains nested values.

Debug output is for inspection. Rust does not promise a stable textual Debug representation, so do not parse it, persist it as a file format, or depend on its exact punctuation.

If the vector represents text that users should read, choose the presentation yourself instead of exposing Debug output. For a list of strings, joining the elements is one simple option:

EXAMPLE: CI-CHECKEDTEST ASSERTED · examples/format-output/src/print_values_pages.rs#debug-vec-user-output
let names = vec!["Ada", "Linus", "Grace"];
println!("{}", names.join(", "));

For custom element types, implement or use their intended Display representation and decide how separators, brackets, labels, or localization should work at the collection level.

For the broader distinction, see Debug vs Display.