I have the following code that does not compile.
fn main() { let a = "123" .chars() .chain("4566".chars()) .zip( "bbb" .chars() .chain("yyy".chars())) .rev() .map(|x, y| y) .collect::<String>(); println!("Hello, world! {}", a); }
An error has appeared, for example:
src/main.rs:37:10: 37:15 error: the trait `core::iter::ExactSizeIterator` is not implemented for the type `core::iter::Chain<core::str::Chars<'_>, core::str::Chars<'_>>` [E0277] src/main.rs:37 .rev() ^~~~~ src/main.rs:37:10: 37:15 error: the trait `core::iter::ExactSizeIterator` is not implemented for the type `core::iter::Chain<core::str::Chars<'_>, core::str::Chars<'_>>` [E0277] src/main.rs:37 .rev() ^~~~~ src/main.rs:38:10: 38:23 error: type `core::iter::Rev<core::iter::Zip<core::iter::Chain<core::str::Chars<'_>, core::str::Chars<'_>>, core::iter::Chain<core::str::Chars<'_>, core::str::Chars<'_>>>>` does not implement any method in scope named `map` src/main.rs:38 .map(|x, y| y)
My understanding of the rev() method is defined in Iterator as where it implements the DoubleEndedIterator property
fn rev(self) -> Rev<Self> where Self: DoubleEndedIterator { ... }
Zip also implements this feature:
impl<A, B> DoubleEndedIterator for Zip<A, B> where B: DoubleEndedIterator + ExactSizeIterator, A: DoubleEndedIterator + ExactSizeIterator
Therefore, the Chain problem does not implement ExactSizeIterator . But how do I get around this?
I tried adding .take() for both chains to convert the type to Take , which implements ExactSizeIterator , but Take does not implement DoubleEndedIterator .
Please note that this is a simplified example. In fact, I can not change both chains first, and then make zip.
rust
Changgeng
source share