Ho il seguente codice che non viene compilato.Come invertire dopo zip due catene
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);
}
errore Got come segue:
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)
La mia comprensione è rev()
metodo è definito in Iterator
esempio dove implementa tratto DoubleEndedIterator
fn rev(self) -> Rev<Self> where Self: DoubleEndedIterator { ... }
anche Zip
implementa questa caratteristica:
impl<A, B> DoubleEndedIterator for Zip<A, B> where B: DoubleEndedIterator + ExactSizeIterator, A: DoubleEndedIterator + ExactSizeIterator
Quindi il problema è Chain
non implementa ExactSizeIterator
. Ma come posso aggirare questo?
Ho provato ad aggiungere .take()
per entrambe le catene per convertire il tipo in Take
che implementa ExactSizeIterator
, ma Take non implementa DoubleEndedIterator
.
Nota questo è un esempio semplificato. In realtà non posso invertire prima entrambe le catene e poi fare lo zip.