As title, in Rust, .rev().rev() works, .rev().skip(1) works, but .rev().skip(1).rev() does not. The following is the demonstration:
// This compiles
fn main() {
let s = "Hello!";
println!("{}", &s.chars().rev().skip(1).collect::<String>());
}
// This compiles
fn main() {
let s = "Hello!";
println!("{}", &s.chars().rev().rev().collect::<String>());
}
// This *does not* compile
fn main() {
let s = "Hello!";
println!("{}", &s.chars().rev().skip(1).rev().collect::<String>());
}
The last one does not compile:
error[E0277]: the trait bound `Chars<'_>: ExactSizeIterator` is not satisfied
--> src/main.rs:3:45
|
3 | println!("{}", &s.chars().rev().skip(1).rev().collect::<String>());
| ^^^ the trait `ExactSizeIterator` is not implemented for `Chars<'_>`
|
= note: required because of the requirements on the impl of `ExactSizeIterator` for `Rev<Chars<'_>>`
= note: required because of the requirements on the impl of `DoubleEndedIterator` for `Skip<Rev<Chars<'_>>>`
error[E0599]: the method `collect` exists for struct `Rev<Skip<Rev<Chars<'_>>>>`, but its trait bounds were not satisfied
--> src/main.rs:3:51
|
3 | println!("{}", &s.chars().rev().skip(1).rev().collect::<String>());
| ^^^^^^^ method cannot be called on `Rev<Skip<Rev<Chars<'_>>>>` due to unsatisfied trait bounds
|
= note: the following trait bounds were not satisfied:
`Skip<Rev<Chars<'_>>>: DoubleEndedIterator`
which is required by `Rev<Skip<Rev<Chars<'_>>>>: Iterator`
`Rev<Skip<Rev<Chars<'_>>>>: Iterator`
which is required by `&mut Rev<Skip<Rev<Chars<'_>>>>: Iterator`
Could someone explain why this is the case?