Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print a Vec?

Tags:

rust

println

I tried the following code:

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

But the compiler complains:

error[E0277]: `std::vec::Vec<{integer}>` doesn't implement `std::fmt::Display`  --> src/main.rs:3:20   | 3 |     println!("{}", v2);   |                    ^^ `std::vec::Vec<{integer}>` cannot be formatted with the default formatter   |   = help: the trait `std::fmt::Display` is not implemented for `std::vec::Vec<{integer}>`   = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead   = note: required by `std::fmt::Display::fmt` 

Does anyone implement this trait for Vec<T>?

like image 488
highfly22 Avatar asked May 19 '15 08:05

highfly22


People also ask

Can you print a vector in C++?

But using C++ 17 experimental::make_ostream_joiner, it is possible to print all elements of a vector without specifying the type of elements in the vector while calling copy().

How do you print a vector content in Rust?

let v2 = vec! [1; 10]; println!

How do I show a vector in C++?

In the for loop, size of vector is calculated for the maximum number of iterations of loop and using at(), the elements are printed. for(int i=0; i < a. size(); i++) std::cout << a.at(i) << ' '; In the main() function, the elements of vector are passed to print them.


2 Answers

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

like image 109
Manishearth Avatar answered Sep 21 '22 15:09

Manishearth


Does anyone implement this trait for Vec<T> ?

No.

And surprisingly, this is a demonstrably correct answer; which is rare since proving the absence of things is usually hard or impossible. So how can we be so certain?

Rust has very strict coherence rules, the impl Trait for Struct can only be done:

  • either in the same crate as Trait
  • or in the same crate as Struct

and nowhere else; let's try it:

impl<T> std::fmt::Display for Vec<T> {     fn fmt(&self, _: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {         Ok(())     } } 

yields:

error[E0210]: type parameter `T` must be used as the type parameter for some local type (e.g., `MyStruct<T>`)  --> src/main.rs:1:1   | 1 | impl<T> std::fmt::Display for Vec<T> {   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type parameter `T` must be used as the type parameter for some local type   |   = note: only traits defined in the current crate can be implemented for a type parameter 

Furthermore, to use a trait, it needs to be in scope (and therefore, you need to be linked to its crate), which means that:

  • you are linked both with the crate of Display and the crate of Vec
  • neither implement Display for Vec

and therefore leads us to conclude that no one implements Display for Vec.


As a work around, as indicated by Manishearth, you can use the Debug trait, which is invokable via "{:?}" as a format specifier.

like image 44
Matthieu M. Avatar answered Sep 21 '22 15:09

Matthieu M.