2020-06-23 19:23:53 -05:00
|
|
|
// run-pass
|
|
|
|
|
2021-03-04 07:35:11 -06:00
|
|
|
// compile-flags: -Zmir-opt-level=3
|
2020-06-23 19:23:53 -05:00
|
|
|
|
|
|
|
trait IterExt: Iterator {
|
|
|
|
fn fold_ex<B, F>(mut self, init: B, mut f: F) -> B
|
|
|
|
where
|
|
|
|
Self: Sized,
|
|
|
|
F: FnMut(B, Self::Item) -> B,
|
|
|
|
{
|
|
|
|
let mut accum = init;
|
|
|
|
while let Some(x) = self.next() {
|
|
|
|
accum = f(accum, x);
|
|
|
|
}
|
|
|
|
accum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Iterator> IterExt for T {}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let test = &["\n"];
|
|
|
|
test.iter().fold_ex(String::new(), |_, b| b.to_string());
|
|
|
|
}
|