diff options
| author | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-10-25 15:21:49 +0200 |
|---|---|---|
| committer | Ulrik Sverdrup <bluss@users.noreply.github.com> | 2016-10-25 22:06:39 +0200 |
| commit | a16626fc422f9fdcd1d02f56b628f764d5282261 (patch) | |
| tree | 2906da152b755dd46bef34a1f9361b75128650a4 /src/libcoretest | |
| parent | 780acda325772b15f12f08f60ca2d4ba558cee51 (diff) | |
| download | rust-a16626fc422f9fdcd1d02f56b628f764d5282261.tar.gz rust-a16626fc422f9fdcd1d02f56b628f764d5282261.zip | |
iter: Implement .fold() for .chain()
Chain can do something interesting here where it passes on the fold into its inner iterators. The lets the underlying iterator's custom fold() be used, and skips the regular chain logic in next.
Diffstat (limited to 'src/libcoretest')
| -rw-r--r-- | src/libcoretest/iter.rs | 12 |
1 files changed, 12 insertions, 0 deletions
diff --git a/src/libcoretest/iter.rs b/src/libcoretest/iter.rs index 27eb25537f3..58b6444ef88 100644 --- a/src/libcoretest/iter.rs +++ b/src/libcoretest/iter.rs @@ -985,6 +985,18 @@ fn test_empty() { assert_eq!(it.next(), None); } +#[test] +fn test_chain_fold() { + let xs = [1, 2, 3]; + let ys = [1, 2, 0]; + + let mut iter = xs.iter().chain(&ys); + iter.next(); + let mut result = Vec::new(); + iter.fold((), |(), &elt| result.push(elt)); + assert_eq!(&[2, 3, 1, 2, 0], &result[..]); +} + #[bench] fn bench_rposition(b: &mut Bencher) { let it: Vec<usize> = (0..300).collect(); |
