@@ -2,12 +2,13 @@ use crate::cmp::{self, Ordering};
22use crate :: ops:: { ChangeOutputType , ControlFlow , FromResidual , Residual , Try } ;
33
44use super :: super :: TrustedRandomAccessNoCoerce ;
5+ use super :: super :: {
6+ ArrayWindows , Inspect , Map , MapWhile , Peekable , Rev , Scan , Skip , SkipWhile , StepBy , Take ,
7+ TakeWhile ,
8+ } ;
59use super :: super :: { Chain , Cloned , Copied , Cycle , Enumerate , Filter , FilterMap , Fuse } ;
610use super :: super :: { FlatMap , Flatten } ;
711use super :: super :: { FromIterator , Intersperse , IntersperseWith , Product , Sum , Zip } ;
8- use super :: super :: {
9- Inspect , Map , MapWhile , Peekable , Rev , Scan , Skip , SkipWhile , StepBy , Take , TakeWhile ,
10- } ;
1112
1213fn _assert_is_object_safe ( _: & dyn Iterator < Item = ( ) > ) { }
1314
@@ -3057,6 +3058,49 @@ pub trait Iterator {
30573058 Cycle :: new ( self )
30583059 }
30593060
3061+ /// Returns an iterator over all contiguous windows of length `N`. The
3062+ /// windows overlap. If the iterator is shorter than `N`, the iterator
3063+ /// returns no values.
3064+ ///
3065+ /// `array_windows` clones the iterator elements so that they can be part of
3066+ /// successive windows, this makes this it most suited for iterators of
3067+ /// references and other values that are cheap to clone.
3068+ ///
3069+ /// # Panics
3070+ ///
3071+ /// If called with `N = 0`.
3072+ ///
3073+ /// # Examples
3074+ ///
3075+ /// Basic usage:
3076+ ///
3077+ /// ```
3078+ /// #![feature(iter_array_windows)]
3079+ ///
3080+ /// let mut iter = "rust".chars().array_windows();
3081+ /// assert_eq!(iter.next(), Some(['r', 'u']));
3082+ /// assert_eq!(iter.next(), Some(['u', 's']));
3083+ /// assert_eq!(iter.next(), Some(['s', 't']));
3084+ /// assert_eq!(iter.next(), None);
3085+ /// ```
3086+ ///
3087+ /// ```
3088+ /// #![feature(iter_array_windows)]
3089+ ///
3090+ /// let seq: &[i32] = &[0, 1, 1, 2, 3, 5, 8, 13];
3091+ /// for [x, y, z] in seq.iter().copied().array_windows() {
3092+ /// assert_eq!(x + y, z);
3093+ /// }
3094+ /// ```
3095+ #[ unstable( feature = "iter_array_windows" , reason = "recently added" , issue = "none" ) ]
3096+ fn array_windows < const N : usize > ( self ) -> ArrayWindows < Self , N >
3097+ where
3098+ Self : Sized ,
3099+ Self :: Item : Clone ,
3100+ {
3101+ ArrayWindows :: new ( self )
3102+ }
3103+
30603104 /// Sums the elements of an iterator.
30613105 ///
30623106 /// Takes each element, adds them together, and returns the result.
0 commit comments