How to print a Vec?

let v2 = vec![1; 10];
println!("{:?}", v2);

{} is for strings and other values which can be displayed directly to the user. There’s no single way to show a vector to a user.

The {:?} formatter can be used to debug it, and it will look like:

[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

Display is the trait that provides the method behind {}, and Debug is for {:?}

Leave a Comment