mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-24 07:44:10 +00:00
25 lines
482 B
Rust
25 lines
482 B
Rust
//@ run-pass
|
|
|
|
//@ compile-flags: -Zmir-opt-level=3
|
|
|
|
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());
|
|
}
|