How to print a Vec?

后端 未结 5 1837
Happy的楠姐
Happy的楠姐 2020-12-03 00:21

I tried the following code:

fn main() {
    let v2 = vec![1; 10];
    println!(\"{}\", v2);
}

But the compiler complains:



        
5条回答
  •  时光取名叫无心
    2020-12-03 01:06

    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 {:?}

提交回复
热议问题