Error regarding unimplemented trait

Hi fellow yew and rust users,

I have this error

I have already provided implementation for Foo

impl Display for Foo {
    fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
        write!(f, "{}", format!("{} => {}", self.a, self.b))

Any ideas why am i still faced with this error?

To learn more, run the command again with --verbose.
error[E0277]: `std::vec::Vec<core::Foo>` doesn't implement `std::fmt::Display`
   --> pii-yew/src/
144 | /         html! {
145 | |
146 | |             <div>
147 | |                 <Select<CountryCode>:
...   |
172 | |             </div>
173 | |         }
    | |_________^ `std::vec::Vec<core::Foo>` cannot be formatted with the default formatter
    = help: the trait `std::fmt::Display` is not implemented for `std::vec::Vec<core::Foo>`
    = note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
    = note: required because of the requirements on the impl of `std::string::ToString` for `std::vec::Vec<core::Foo>`
    = note: required by `yew::macros::add_attribute`
    = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.

The error is not about your type. It's about a Vec that holds your type.

Vec intentionally refuses to be printed using {}, and there's nothing you can do to force it.

You can print Vec with {:?} to get a debug representation. Or you can iterate your vec and print its content yourself.


Thanks I kinda assumed that as long as the contents have the implementation the collection of it will have the implementation also

That's true for Debug, Clone and a few other traits that have obvious implementations. However Display is for user-oriented display, and the Vec doesn't want to be deciding how to display multiple elements (one per line? comma separated? what if you want to add "and " before the last element?)

1 Like

And note that even if all the struct elements are Debug, you still have to add #[derive(Debug)] for it to be Debug too - this is for limiting the possibility of the code silently breaking its dependencies. The only exceptions available at stable are AFAIK Sync and Send.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.