rust - How to reverse after zip two chains -
i have following code not compile.
fn main() { let = "123" .chars() .chain("4566".chars()) .zip( "bbb" .chars() .chain("yyy".chars())) .rev() .map(|x, y| y) .collect::<string>(); println!("hello, world! {}", a); }
got error following:
src/main.rs:37:10: 37:15 error: trait `core::iter::exactsizeiterator` not implemented type `core::iter::chain<core::str::chars<'_>, core::str::chars<'_>>` [e0277] src/main.rs:37 .rev() ^~~~~ src/main.rs:37:10: 37:15 error: trait `core::iter::exactsizeiterator` not implemented 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<'_>>>>` not implement method in scope named `map` src/main.rs:38 .map(|x, y| y)
my understanding rev()
method defined in iterator
implements trait doubleendediterator
fn rev(self) -> rev<self> self: doubleendediterator { ... }
also zip
implements trait:
impl<a, b> doubleendediterator zip<a, b> b: doubleendediterator + exactsizeiterator, a: doubleendediterator + exactsizeiterator
so problem chain
doesn't implement exactsizeiterator
. how work around this?
i tried add .take()
both chains convert type take
implements exactsizeiterator
, take doesn't implement doubleendediterator
.
note simplified example. in reality cannot reverse both chain first , zip.
you're looking following impl (spoiler: doesn't exist):
impl<a, b> exactsizeiterator chain<a, b> a: exactsizeiterator, b: exactsizeiterator { ... }
an exactsizeiterator
must implement 1 method, len(&self)
. idea behind hypothetical implementation sum both lengths chain_a_b.len() == a.len() + b.len()
.
the reason doesn't exist rust cannot guarantee addition (usize + usize
) not overflow. forbids it. sounds little strict, that's status quo now, unfortunately.
worse: if impl existed, run fact chars
not exactsizeiterator
, still not work.
an alternative (probably not one) collect chains vector. bad because of memory allocation, if isn't perf bottleneck worth tradeoff.
Comments
Post a Comment