You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use std::fmt::{self,Display,Formatter};// This struct provides a `Display` implementation that iterates over// &collection, writing each element in sequence.pubstructConcat<C>{collection:C,}impl<C>DisplayforConcat<C>wherefor<'a>&'aC:IntoIterator,for<'a> <&'aCasIntoIterator>::Item:Display,{fnfmt(&self,f:&mutFormatter) -> fmt::Result{self.collection.into_iter().try_for_each(move |element| {
element.fmt(f)})}}fnmain(){let collection = vec![1u32,2,3,4,5,6];let concat = Concat{ collection };println!("{}", concat);}
We define a struct, Concat, that provides a Display implementation by immutably iterating over collection and writing each element to f in sequence. This trait implementation depends on the following trait bounds:
for<'a> &'aC:IntoIterator,// immutable iterationfor<'a> <&'aCasIntoIterator>::Item:Display// Items are displayable
However, the latter trait bound appears to not be binding correctly, as the example code fails to compile with the following error:
error[E0277]: `<&'a std::vec::Vec<u32> as std::iter::IntoIterator>::Item` doesn't implement `std::fmt::Display`
--> src/main.rs:23:20
|
23 | println!("{}", concat);
| ^^^^^^ `<&'a std::vec::Vec<u32> as std::iter::IntoIterator>::Item` cannot be formatted with the default formatter
|
= help: the trait `for<'a> std::fmt::Display` is not implemented for `<&'a std::vec::Vec<u32> as std::iter::IntoIterator>::Item`
= 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::fmt::Display` for `Concat<std::vec::Vec<u32>>`
= note: required by `std::fmt::Display::fmt`
It's worth noting that the code does compile if you don't include the usage in main. I've tried a lot of variations on this code, but the issue seems to boil down to the fact that Rust seems to consider for<'a> Display different than display.
I posted this issue on stackoverflow, and got a response with the following workaround:
mod private {use std::fmt;pubtraitDisplay<'a>: fmt::Display{}impl<'a,T>Display<'a>forTwhereT: fmt::Display{}}impl<C> fmt::DisplayforConcat<C>wherefor<'a>&'aContainer:IntoIterator,for<'a> <&'aContainerasIntoIterator>::Item: private::Display<'a>,
...
In summary, there appears to be an issue with binding an associated type to a trait in an HRTB context.
The text was updated successfully, but these errors were encountered:
Consider this example (playground):
We define a struct,
Concat
, that provides aDisplay
implementation by immutably iterating overcollection
and writing each element tof
in sequence. This trait implementation depends on the following trait bounds:However, the latter trait bound appears to not be binding correctly, as the example code fails to compile with the following error:
It's worth noting that the code does compile if you don't include the usage in
main
. I've tried a lot of variations on this code, but the issue seems to boil down to the fact that Rust seems to considerfor<'a> Display
different than display.I posted this issue on stackoverflow, and got a response with the following workaround:
In summary, there appears to be an issue with binding an associated type to a trait in an HRTB context.
The text was updated successfully, but these errors were encountered: