From 3cc9052b4c364b31e52df20081fdae69db472afb Mon Sep 17 00:00:00 2001 From: Ivan Tham Date: Thu, 20 Aug 2020 18:56:40 +0800 Subject: [PATCH] Add Iterator::join to combine Iterator and Join Iterator::join will be added as extention trait IteratorExt inside alloc crate as it requries allocation while joining which is the reason why Join avaliable in alloc but not core. It is an easier alternative to `.collect::>().join(sep)`. Tracking issue: https://github.com/rust-lang/rust/issues/75638 --- library/alloc/src/iter.rs | 40 +++++++++++++++++++++++++++++++++++++++ library/alloc/src/lib.rs | 1 + 2 files changed, 41 insertions(+) create mode 100644 library/alloc/src/iter.rs diff --git a/library/alloc/src/iter.rs b/library/alloc/src/iter.rs new file mode 100644 index 0000000000000..e0c2d063e5fcb --- /dev/null +++ b/library/alloc/src/iter.rs @@ -0,0 +1,40 @@ +//! Allocation extensions for [`Iterator`]. +//! +//! *[See also the Iterator trait][Iterator].* +#![unstable(feature = "iterator_join", issue = "75638")] + +use crate::slice::Join; +use crate::vec::Vec; + +/// Iterator extension traits that requires allocation. +#[unstable(feature = "iterator_join", issue = "75638")] +pub trait IteratorExt: Iterator { + /// Flattens an iterator into a single value with the given separator in + /// between. + /// + /// Combines `collect` with `join` to convert a sequence into a value + /// separated with the specified separator. + /// + /// Allows `.join(sep)` instead of `.collect::>().join(sep)`. + /// + /// ``` + /// #![feature(iterator_join)] + /// use alloc::iter::IteratorExt; + /// + /// assert_eq!(["hello", "world"].iter().copied().join(" "), "hello world"); + /// assert_eq!([[1, 2], [3, 4]].iter().copied().join(&0), [1, 2, 0, 3, 4]); + /// assert_eq!([[1, 2], [3, 4]].iter().copied().join(&[0, 0][..]), [1, 2, 0, 0, 3, 4]); + /// ``` + #[inline] + #[unstable(feature = "iterator_join", issue = "75638")] + #[must_use = "if you really need to exhaust the iterator, consider `.for_each(drop)` instead"] + fn join(self, sep: Separator) -> <[Self::Item] as Join>::Output + where + [Self::Item]: Join, + Self: Sized, + { + Join::join(self.collect::>().as_slice(), sep) + } +} + +impl IteratorExt for T {} diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 2d25941a52412..e3a09fff04a58 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -167,6 +167,7 @@ mod boxed { pub mod borrow; pub mod collections; pub mod fmt; +pub mod iter; pub mod prelude; pub mod raw_vec; pub mod rc;