@@ -7,6 +7,7 @@ use super::super::{
77use super :: TrustedLen ;
88use crate :: array;
99use crate :: cmp:: { self , Ordering } ;
10+ use crate :: iter:: adapters:: { Dedup , DedupEq , DedupKey } ;
1011use crate :: num:: NonZero ;
1112use crate :: ops:: { ChangeOutputType , ControlFlow , FromResidual , Residual , Try } ;
1213
@@ -1868,6 +1869,142 @@ pub trait Iterator {
18681869 Inspect :: new ( self , f)
18691870 }
18701871
1872+ /// Removes all but the first of consecutive repeated elements in the iterator
1873+ /// according to the [`PartialEq`] trait implementation.
1874+ ///
1875+ /// For an iterator yielding infinitely many consecutive duplicates,
1876+ /// calling [`next`][Iterator::next] on this iterator may never halt.
1877+ ///
1878+ /// If the iterator is sorted, this removes all duplicates.
1879+ ///
1880+ /// # Examples
1881+ ///
1882+ /// Basic usage:
1883+ ///
1884+ /// ```
1885+ /// #![feature(iter_dedup)]
1886+ ///
1887+ /// let vec = vec![1, 2, 2, 3, 2];
1888+ ///
1889+ /// let mut iter = vec.into_iter().dedup();
1890+ ///
1891+ /// assert_eq!(iter.next(), Some(1));
1892+ /// assert_eq!(iter.next(), Some(2));
1893+ /// assert_eq!(iter.next(), Some(3));
1894+ /// assert_eq!(iter.next(), Some(2));
1895+ /// assert_eq!(iter.next(), None);
1896+ /// ```
1897+ ///
1898+ /// Example of an infinite loop:
1899+ ///
1900+ /// ```no_run
1901+ /// #![feature(iter_dedup)]
1902+ ///
1903+ /// // this will never terminate
1904+ /// let _ = std::iter::repeat(2).dedup().next();
1905+ /// ```
1906+ #[ unstable( feature = "iter_dedup" , issue = "83747" ) ]
1907+ #[ inline]
1908+ fn dedup < F > ( self ) -> Dedup < Self , DedupEq >
1909+ where
1910+ Self : Sized ,
1911+ Self :: Item : PartialEq ,
1912+ {
1913+ Dedup :: new ( self , DedupEq )
1914+ }
1915+
1916+ /// Removes all but the first of consecutive elements in the iterator
1917+ /// satisfying a given equality relation.
1918+ ///
1919+ /// The `same_bucket` function is passed a references to two elements from
1920+ /// the iterator and must determine if the elements compare equal.
1921+ ///
1922+ /// For an iterator yielding infinitely many consecutive duplicates,
1923+ /// calling [`next`][Iterator::next] on this iterator may never halt.
1924+ ///
1925+ /// If the iterator is sorted, this removes all duplicates.
1926+ ///
1927+ /// # Examples
1928+ ///
1929+ /// Basic usage:
1930+ ///
1931+ /// ```
1932+ /// #![feature(iter_dedup)]
1933+ ///
1934+ /// let vec = vec!["foo", "bar", "Bar", "baz", "bar"];
1935+ ///
1936+ /// let mut iter = vec.into_iter().dedup_by(|a, b| a.eq_ignore_ascii_case(b));
1937+ ///
1938+ /// assert_eq!(iter.next(), Some("foo"));
1939+ /// assert_eq!(iter.next(), Some("bar"));
1940+ /// assert_eq!(iter.next(), Some("baz"));
1941+ /// assert_eq!(iter.next(), Some("bar"));
1942+ /// assert_eq!(iter.next(), None);
1943+ /// ```
1944+ ///
1945+ /// Example of an infinite loop:
1946+ ///
1947+ /// ```no_run
1948+ /// #![feature(iter_dedup)]
1949+ ///
1950+ /// // this will never terminate
1951+ /// let _ = std::iter::repeat(2).dedup_by(|a, b| a == b).next();
1952+ /// ```
1953+ #[ unstable( feature = "iter_dedup" , issue = "83747" ) ]
1954+ #[ inline]
1955+ fn dedup_by < F > ( self , f : F ) -> Dedup < Self , F >
1956+ where
1957+ Self : Sized ,
1958+ F : FnMut ( & Self :: Item , & Self :: Item ) -> bool ,
1959+ {
1960+ Dedup :: new ( self , f)
1961+ }
1962+
1963+ /// Removes all but the first of consecutive elements in the iterator
1964+ /// that resolve to the same key.
1965+ ///
1966+ /// For an iterator yielding infinitely many consecutive duplicates,
1967+ /// calling [`next`][Iterator::next] on this iterator may never halt.
1968+ ///
1969+ /// If the iterator is sorted, this removes all duplicates.
1970+ ///
1971+ /// # Examples
1972+ ///
1973+ /// Basic usage:
1974+ ///
1975+ /// ```
1976+ /// #![feature(iter_dedup)]
1977+ ///
1978+ /// let vec = vec![10, 20, 21, 30, 20];
1979+ ///
1980+ /// let mut iter = vec.into_iter().dedup_by_key(|&i| i / 10);
1981+ ///
1982+ /// assert_eq!(iter.next(), Some(10));
1983+ /// assert_eq!(iter.next(), Some(20));
1984+ /// assert_eq!(iter.next(), Some(30));
1985+ /// assert_eq!(iter.next(), Some(20));
1986+ /// assert_eq!(iter.next(), None);
1987+ /// ```
1988+ ///
1989+ /// Example of an infinite loop:
1990+ ///
1991+ /// ```no_run
1992+ /// #![feature(iter_dedup)]
1993+ ///
1994+ /// // this will never terminate
1995+ /// let _ = std::iter::repeat(2).dedup_by_key(|&n| n).next();
1996+ /// ```
1997+ #[ unstable( feature = "iter_dedup" , issue = "83747" ) ]
1998+ #[ inline]
1999+ fn dedup_by_key < F , K > ( self , f : F ) -> Dedup < Self , DedupKey < F > >
2000+ where
2001+ Self : Sized ,
2002+ F : FnMut ( & Self :: Item ) -> K ,
2003+ K : PartialEq ,
2004+ {
2005+ Dedup :: new ( self , DedupKey ( f) )
2006+ }
2007+
18712008 /// Creates a "by reference" adapter for this instance of `Iterator`.
18722009 ///
18732010 /// Consuming method calls (direct or indirect calls to `next`)
0 commit comments